在 Visual Studio 2008 下
我可以创建一个扩展方法以在 .NET Framework 2.0 项目下工作吗?
最佳答案
有一个ugly hack获取在 .Net 2.0 中工作的扩展方法;但最好只将您的框架升级到 3.5。
简而言之(来自链接 #2):扩展方法只是标有 [Extension] 属性的普通静态方法。这个属性实际上只是由编译器在幕后添加的。在 .NET 3.5 中,它存在于 System.Core 中,因此只需像这样定义您自己的属性:
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class ExtensionAttribute : Attribute
{
}
}
关于c# - 在 .NET Framework 2.0 中使用扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/783155/