Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 937
Amr Avatar asked Apr 23 '09 19:04

Amr


People also ask

Should I use extension methods C#?

Extension methods are an excellent addition to the C# language. They enable us to write nicer, more readable code. They allow for more functionally styled programming, which is very much needed in an object-oriented language. They also should be used with care.

Where we can use extension method in C#?

You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself.

Where do you put extension methods?

An Extension Method should be in the same namespace as it is used or you need to import the namespace of the class by a using statement. You can give any name of for the class that has an Extension Method but the class should be static.

Can you add extension methods to an existing static class?

The main advantage of the extension method is to add new methods in the existing class without using inheritance. You can add new methods in the existing class without modifying the source code of the existing class. It can also work with sealed class.


2 Answers

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
  {
  }
}
like image 66
Tom Ritter Avatar answered Sep 20 '22 02:09

Tom Ritter


Absolutely. There are a few hacky methods, but the one I'm using is to take System.Core from the Mono project, add all of its code to a new .NET 2.0 Class Library named System.Core in my own solution, and recompile it. There are a few things to fix, like changing their MonoTODO attributes to TODO comments, and fixing the AssemblyInfo.cs, but it works great. I'm now using both LINQ and extension methods in a 2.0 project compiled in VS 2008.

Assuming you get the 2.4 version of the Mono source, you should find the code under:

<extracted directory>/mono-2.4/mcs/class/System.Core

If you're stuck in VS 2005, you can download SharpDevelop, build your System.Core dll with that targeted to 2.0, add a reference to the compiled assembly, and it may work, but I don't know if VS 2005 will have a problem with the extension syntax or not. I imagine it will give you some lip.

like image 21
Chris Doggett Avatar answered Sep 22 '22 02:09

Chris Doggett