Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create Extension Methods with 2.0 Framework?

I was wondering if there is a way to create extension methods using Visual Studio 2005 and the 2.0 framework?

public static class StringExtensions
{
    public static void SomeExtension(this String targetString)
    {

    }
}

If there is no way to do this, what would the equivalent be? Just create static methods in some sort of library class?

like image 879
DevDemon Avatar asked Feb 17 '10 12:02

DevDemon


1 Answers

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.

like image 55
Pop Catalin Avatar answered Oct 11 '22 23:10

Pop Catalin