Click here to Skip to main content
13,297,048 members (56,398 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 20 Dec 2012

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


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.


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.


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


About the Author

Team Leader
China China
I am now programming applications for the Internet of Things.

You may also be interested in...


Comments and Discussions

GeneralNothing new Pin
PIEBALDconsult15-Jun-13 6:43
professionalPIEBALDconsult15-Jun-13 6:43 
QuestionStill requires newer compiler Pin
Brad Bruce2-Mar-13 3:45
memberBrad Bruce2-Mar-13 3:45 
AnswerRe: Still requires newer compiler Pin
wmjordan2-Mar-13 3:56
memberwmjordan2-Mar-13 3:56 
GeneralRe: Still requires newer compiler Pin
Brad Bruce2-Mar-13 4:48
memberBrad Bruce2-Mar-13 4:48 
GeneralRe: Still requires newer compiler Pin
wmjordan2-Mar-13 19:47
memberwmjordan2-Mar-13 19:47 
QuestionMy 5 Pin
Cezar Gradinariu28-Jan-13 23:41
memberCezar Gradinariu28-Jan-13 23:41 
QuestionMy 5! Pin
Zoltán Zörgő21-Dec-12 3:00
memberZoltán Zörgő21-Dec-12 3:00 
I gave you a 5 not because of the novelty of the information in the article, just because You was not undeterred from implementing although this feature claimed to be part of a higher version, in a lower version. And tough you demonstrated, that there is still a great piece of "marketing" around there. I am wondering what would be the whole list of fw3.5 features that could be used with just such simple tricks.
AnswerRe: My 5! Pin
wmjordan21-Dec-12 16:04
memberwmjordan21-Dec-12 16: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 22 Feb 2013
Article Copyright 2012 by wmjordan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid