Search code examples
c#visual-studio-2008.net-2.0extension-methods

Using Extension Methods with .NET Framework 2.0


Under Visual Studio 2008
Can I create an Extension Method to work under a .NET Framework 2.0 project?


Solution

  • 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.

    Alternate Sources: 1, 2.

    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
      {
      }
    }