views:

496

answers:

3

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

+6  A: 

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

Tom Ritter
+2  A: 

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.

Chris Doggett
The key is compiled in VS 2008. I'm still stuck with VS 2005 :(
Joel Coehoorn
I saw the VS2008 tag and assumed 2008. If you're in 2005, you may be able to find a pre-compiled binary, or use SharpDevelop (I'll update with a link). If you're compiling in Win2K, you're out of luck, unfortunately, since it won't support the 3.5 framework.
Chris Doggett
+1  A: 

See my blog entry for details.

Colin Desmond