When I try to add an extension method using the .NET 2.0 or 3.0 runtime, I get the error:
Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute' cannot be found. Are you missing a reference to System.Core.dll?
But I can't find System.Core in the list of available references when I try to add it to the project. What do I need to do to be able to use extension methods and in turn LINQ on in my projects?
Extension methods were not added to .NET until 3.5. However, it was not a change to the CLR, but a change to the compiler that added them, so you can still use them in your 2.0 and 3.0 projects! The only requirement is you must have a compiler that can create 3.5 projects to be able to do this workaround (Visual Studio 2008 and above).
The error you get when you attempt to use an extension method is misleading as you do not truly need
System.Core.dll
to use extension methods. When you use a extension method, behind the scenes, the compiler is adding the[Extension]
attribute to the function. If you have a compiler that understands what to do with the[Extension]
attribute you can use it in your 2.0 and 3.0 projects if you create the attribute yourself.Just add the following class to your project and you can then start using extension methods:
The above code block is sitting inside System.Core.Dll, so that is why the error says you need to include the DLL file to use them.
Now if you want LINQ functionality that will take a little extra work. You will need to re-implement the extension methods yourself. To mimic the full LINQ to SQL functionality the code can get quite complicated. However, if you are just using LINQ to Objects most LINQ methods are not complicated to implement. Here are a few LINQ to Objects replacement functions from a project I wrote out to get you started.
A library with the full re-implemenation of LINQ to Objects with the
ExtensionAttribute
already added in can be found in the LinqBridge project (Thanks Allon Guralnek).