Introduction
Extension methods were invented since .NET 3.5. However, they are not available when your compiler is targeting .NET 2.0. It appears that .NET .35 is the prerequisite of extension methods and you have to install that 100MB+ runtime on Windows XP or 2003 in order to make the compiled assembly work. Actually, it is not true. This tip is about how to use extension methods in your project and have it run on .NET 2.0 machines.
Background
Extension methods are actually static
methods introduced in static
classes, having a this
keyword before the first method parameter, like in the following code:
public static string Quote (this string text) {
return String.Concat ("\"", text, "\"");
}
You can use the above extension method as if it is a built-in method of the string
class.
string t = "test";
string q = t.Quote (); string n = null;
n = n.Quote ();
However, when you compile the C# code files with extension methods targeting .NET 2.0, you will get an error, saying that "CS1110: Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute
'." The error won't disappear until you target your project back to .NET 3.5.
Using the Code
Actually, extension methods can be compiled by the C# .NET 2.0 SP2 compiler. What you need to do is simply adding the following class to your project:
using System;
#pragma warning disable 1685
namespace System.Runtime.CompilerServices
{
[AttributeUsage (AttributeTargets.Method |
AttributeTargets.Class | AttributeTargets.Assembly)]
public sealed class ExtensionAttribute : Attribute
{
}
}
With this mimic System.Runtime.CompilerServices.ExtensionAttribute
class in your project, its compilation will succeed even when targeting .NET 2.0.
Note: Since our mimic ExtensionAttribute
collides with the one in the global assembly cache, the compiler will generate a warning CS1685. We are of course deliberately doing this. Thus, we add the line of #pragma
to suppress that warning.
How Did I Find This?
When I tried to compile my projects with extension methods in Visual Studio 2008, the compiler threw an error and mentioned the System.Runtime.CompilerServices.ExtensionAttribute
type. Since .NET 3.5 and .NET 2.0 use the same common language runtime, some people suggested extracting and copying the System.Core.dll file, which contains the ExtensionAttribute
class, to the project folder and reference it. It worked. The project could target .NET 2.0 only but our extension methods could run with nothing wrong. Therefore, I wondered, instead of referencing the official System.Core.dll, whether it is possible to mimic my own ExtensionAttribute
class and deceive the compiler.
I then opened Reflector and loaded the System.Core.dll. After dissembling the ExtensionAttribute
class, I copied the code into my project, and the project was successfully compiled and ran perfectly.
By this means, we can enjoy the convenience of extension methods, easily fool the compiler with our own ExtensionAttribute
class. Although we still have to install the big .NET 3.5 on our machine in order to make VS 2008 run, it saves us the trouble of installing .NET 3.5 on Windows XP or 2003 client machines.