[英]Using extension methods in .NET 2.0?
I want to do this, but getting this error: 我想这样做,但得到这个错误:
Error 1 Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute' cannot be found.
错误1无法定义新的扩展方法,因为无法找到编译器所需的类型“System.Runtime.CompilerServices.ExtensionAttribute”。 Are you missing a reference to System.Core.dll?
您是否缺少对System.Core.dll的引用? [snipped some path stuff]
[剪了一些路径]
I have seen some answers here that says, you have to define this attribute yourself. 我在这里看到一些答案说,你必须自己定义这个属性。
How do I do that? 我怎么做?
EDIT : This is what I have: 编辑 :这就是我所拥有的:
[AttributeUsage ( AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method )]
public sealed class ExtensionAttribute : Attribute
{
public static int MeasureDisplayStringWidth ( this Graphics graphics, string text )
{
}
}
Like so: 像这样:
// you need this once (only), and it must be in this namespace
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class
| AttributeTargets.Method)]
public sealed class ExtensionAttribute : Attribute {}
}
// you can have as many of these as you like, in any namespaces
public static class MyExtensionMethods {
public static int MeasureDisplayStringWidth (
this Graphics graphics, string text )
{
/* ... */
}
}
Alternatively; 另外; just add a reference to LINQBridge .
只需添加对LINQBridge的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.