Click here to Skip to main content
Click here to Skip to main content

Using Extension Methods in .NET 2.0 SP2

, 22 Feb 2013
Rate this:
Please Sign up or sign in to vote.
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:

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 (); // 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:

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)

About the Author

wmjordan

China China
Chinese Poetry Lover.
Programmer.

Comments and Discussions

 
GeneralNothing new PinprofessionalPIEBALDconsult15-Jun-13 5:43 
QuestionStill requires newer compiler PinmemberBrad Bruce2-Mar-13 2:45 
AnswerRe: Still requires newer compiler [modified] Pinmemberwmjordan2-Mar-13 2:56 
GeneralRe: Still requires newer compiler PinmemberBrad Bruce2-Mar-13 3:48 
GeneralRe: Still requires newer compiler Pinmemberwmjordan2-Mar-13 18:47 
QuestionMy 5 PinmemberCezar Gradinariu28-Jan-13 22:41 
QuestionMy 5! PinmemberZoltán Zörgő21-Dec-12 2:00 
AnswerRe: My 5! Pinmemberwmjordan21-Dec-12 15:04 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 22 Feb 2013
Article Copyright 2012 by wmjordan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid