gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:07:37 25 4
gpt4 key购买 nike

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com