使用带有.NET Framework 2.0的扩展方法 [英] Using Extension Methods with .NET Framework 2.0
问题描述
在Visual Studio 2008中
我可以创建一个扩展方法一的.NET Framework 2.0的项目下工作?
Under Visual Studio 2008
Can I create an Extension Method to work under a .NET Framework 2.0 project?
推荐答案
有一个的丑陋的黑客,获取扩展方法.NET 2.0中的工作; 。但它只是为了更好地你的框架升级到3.5
There is an ugly hack that gets Extension methods working in .Net 2.0; but it would better just to upgrade your framework to 3.5.
在短(从链接#2):扩展方法是标记了[扩展]属性只是正常的静态方法。此属性实际上只是在幕后编译器增加。在.NET 3.5,它生活在System.Core程序,所以只要定义自己的属性是这样的:
In short (from link #2): Extension methods are just normal static methods tagged with the [Extension] attribute. This attribute is actually just added by the compiler behind the scenes. In .NET 3.5, it lives in System.Core, so just define your own attribute like this:
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class ExtensionAttribute : Attribute
{
}
}
这篇关于使用带有.NET Framework 2.0的扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!