Click here to Skip to main content
15,884,425 members
Articles / Programming Languages / C#
Tip/Trick

Using Extension Methods in .NET 2.0 SP2

Rate me:
Please Sign up or sign in to vote.
4.33/5 (5 votes)
22 Feb 2013CPOL2 min read 30.2K   16   8
Compile extension method targeting .NET 2.0 only

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:

C#
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.

C#
string t = "test";
string q = t.Quote (); // Quote appeared to be a method of System.String
string n = null;
n = n.Quote (); // This won't throw the NullReferenceException

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:

C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
China China
I am now programming applications for the Internet of Things.

Comments and Discussions

 
GeneralNothing new Pin
PIEBALDconsult15-Jun-13 5:43
mvePIEBALDconsult15-Jun-13 5:43 
QuestionStill requires newer compiler Pin
Brad Bruce2-Mar-13 2:45
Brad Bruce2-Mar-13 2:45 
AnswerRe: Still requires newer compiler Pin
wmjordan2-Mar-13 2:56
professionalwmjordan2-Mar-13 2:56 
GeneralRe: Still requires newer compiler Pin
Brad Bruce2-Mar-13 3:48
Brad Bruce2-Mar-13 3:48 
GeneralRe: Still requires newer compiler Pin
wmjordan2-Mar-13 18:47
professionalwmjordan2-Mar-13 18:47 
QuestionMy 5 Pin
Cezar Gradinariu28-Jan-13 22:41
Cezar Gradinariu28-Jan-13 22:41 
QuestionMy 5! Pin
Zoltán Zörgő21-Dec-12 2:00
Zoltán Zörgő21-Dec-12 2:00 
AnswerRe: My 5! Pin
wmjordan21-Dec-12 15:04
professionalwmjordan21-Dec-12 15:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.