Is it possible to create Extension Methods with 2.0 Framework?

♀尐吖头ヾ 提交于 2019-12-05 01:17:04

You can create extension methods using .Net framework 2.0, if you use the C# 3.0 compiler and Visual Studio 2008 or greater.

The catch is that you have to add this code to your project:

 namespace System.Runtime.CompilerServices
{
  public class ExtensionAttribute : Attribute { }
}

Basically you need to re declare the ExtensionAttribute in Core.dll (.Net 3.5 +), in your project.

No, this isn't possible in .Net 2.0 (without using the C# 3.0 compiler). You can just create static methods that do exactly the same thing however:

public static class StringExtensions
{
    public static void SomeExtension(String targetString)
    {
        // Do things
    }
}

// Example use:
StringExtensions.SomeExtension(targetString);

In reality extension methods are just a shorthand way of writing the above.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!