使用带有.NET Framework 2.0的扩展方法 [英] Using Extension Methods with .NET Framework 2.0

查看:357
本文介绍了使用带有.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.

替代能源:的 1 ,的 2

在短(从链接#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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆