c# - 在 .NET Framework 2.0 中使用扩展方法

标签 c# visual-studio-2008 .net-2.0 extension-methods

在 Visual Studio 2008 下
我可以创建一个扩展方法以在 .NET Framework 2.0 项目下工作吗?

最佳答案

有一个ugly hack获取在 .Net 2.0 中工作的扩展方法;但最好只将您的框架升级到 3.5。

替代来源:1 , 2 .

简而言之(来自链接 #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/

相关文章:

c# - 在 C# 中重写面板的控件属性

c# - 如何在未使用 c# 加入 Microsoft Active Directory 中的域时对 ldap 进行身份验证

c# - SharePoint 错误 : Web application at xxxx could not be found

C# 如何在命令行应用程序中接收系统关闭或退出事件

vb.net - 在标签中显示 MySQL 数据 - Vb.Net

c# - 我的服务卸载失败

c# - LINQ First/FirstOrDefault 是否迭代整个可枚举?

c# - 当前上下文中不存在该名称(变量)

visual-studio-2008 - 如何使SSIS约束线保持整洁?

.net - 使用 .Net OnSerializingAttribute 更改变量名称?