[英]Using extension methods in .NET 2.0?
我想這樣做,但得到這個錯誤:
錯誤1無法定義新的擴展方法,因為無法找到編譯器所需的類型“System.Runtime.CompilerServices.ExtensionAttribute”。 您是否缺少對System.Core.dll的引用? [剪了一些路徑]
我在這里看到一些答案說,你必須自己定義這個屬性。
我怎么做?
編輯 :這就是我所擁有的:
[AttributeUsage ( AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method )]
public sealed class ExtensionAttribute : Attribute
{
public static int MeasureDisplayStringWidth ( this Graphics graphics, string text )
{
}
}
像這樣:
// 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 )
{
/* ... */
}
}
另外; 只需添加對LINQBridge的引用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.