Welcome to MSDN Blogs Sign in | Join | Help

jaredpar's WebLog

Code, rants and ramblings of a programmer.
Extension Methods without 3.5 Framework

For a time I've been avoiding extension methods.  Not because I'm opposed to using them but because of the 3.5 Framework. 

A lot of the tools I own are designed to be very light weight tools that only require the user to have 2.0 installed on their machine.  I find that the easier that tools are to install, the more likely people are to use them. 

Extension methods require the ExtensionAttribute be available.  Since the attribute is declared in a 3.5 Framework assembly it's not possible to use extension methods without the 3.5 framework.  At least, that's what I thought up until I read an recent MSDN article.

You can simply define the ExtensionAttribute in your assembly and extension methods will start working.  No references to the 3.5 framework required.  It's a lightweight solution that adds the full power of extension methods to your program.

Namespace System.Runtime.CompilerServices
    Class ExtensionAttribute
        Inherits Attribute
    End Class
End Namespace
Posted: Friday, November 16, 2007 3:37 PM by Jared Parsons
Filed under: ,

Comments

Adrian Anttila said:

Could you provide a sample implementation?  I've tried to implement this in C#, and I'm getting "Type expected" compiler errors because of the use of the this keyword in following code:

public static bool IsEmailAddress(this string s)

You're also unclear if the ExtensionAttribute should decorate anything.

Is this all available in MSDN article you mention?  If so, what's the URL?

# November 16, 2007 5:50 PM

Jared Parsons said:

Sorry for the confusion.  Below is some sample code detailing how to get this to work in C#.  For C# you don't need to decorate anything with the Extension attribute (C# has builtin syntax for extension methods).  

using System;

using System.Collections.Generic;

using System.Text;

namespace System.Runtime.CompilerServices

{

   class ExtensionAttribute : Attribute

   {

   }

}

namespace ConsoleApplication168

{

   public static class ExtensionHolder

   {

       public static bool IsEmailAddress(this string s)

       {

           return false;

       }

   }

   class Program

   {

       static void Main(string[] args)

       {

       }

   }

}

# November 16, 2007 6:00 PM

Adrian Anttila said:

Here's my implementation, which still gets the "Type expected" compiler error in Visual Studio 2005:

using System;

using System.Runtime.CompilerServices;

using System.Text.RegularExpressions;

namespace System.Runtime.CompilerServices

{

   public class ExtensionAttribute : Attribute

   {

   }

}

namespace ConsoleApplication1

{

   public static class StringExtensions

   {

       public static bool IsEmailAddress(this string s)

       {

           Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");

           return regex.IsMatch(s);

       }

   }

   public class Program

   {

       public static void Main(string[] args)

       {

           string email = "aanttila@vertigo.com";

           if (email.IsEmailAddress())

           {

               Console.Write("Valid");

           }

           else

           {

               Console.Write("Invalid");

           }

           Console.WriteLine();

           Console.Write("Press <enter> to exit...");

           Console.ReadLine();

       }

   }

}

Are you targeting the 2.0 runtime from VS2008?  I wonder if the compiler in VS2005 won't support the new 3.0 language features even if they are valid for 2.0.

# November 16, 2007 6:29 PM

Adrian Anttila said:

Here's my implementation, which still gets the "Type expected" compiler error in Visual Studio 2005:

using System;

using System.Runtime.CompilerServices;

using System.Text.RegularExpressions;

namespace System.Runtime.CompilerServices

{

   public class ExtensionAttribute : Attribute

   {

   }

}

namespace ConsoleApplication1

{

   public static class StringExtensions

   {

       public static bool IsEmailAddress(this string s)

       {

           Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");

           return regex.IsMatch(s);

       }

   }

   public class Program

   {

       public static void Main(string[] args)

       {

           string email = "aanttila@vertigo.com";

           if (email.IsEmailAddress())

           {

               Console.Write("Valid");

           }

           else

           {

               Console.Write("Invalid");

           }

           Console.WriteLine();

           Console.Write("Press <enter> to exit...");

           Console.ReadLine();

       }

   }

}

Are you targeting the 2.0 runtime from VS2008?  I wonder if the compiler in VS2005 won't support the new 3.0 language features even if they are valid for 2.0.

# November 16, 2007 6:29 PM

Tony said:

Actually, the C# version you wrote still will not compile because of that same error message.

# November 16, 2007 6:30 PM

Jared Parsons said:

This trick will only work with VS2008.  

# November 16, 2007 6:36 PM

Adrian H. said:

扩展方法本质上只是一个编译器级别的语法糖, 但不引用.NET Framework 3.5的程序集却无法发布程序到2.0/3.0版本的运行环境中, 因为它将使那些方法(扩展方法)带上ExtensionAttribute属性, 而就是ExtensionAttribute这个类却存在于.NET Framework 3.5的程序集中. 其实只要使用一个小技巧即可以保证带有扩展方法的程序在Target到.NET Framework 2.0/3.0时通过编译...

# November 17, 2007 9:19 AM

alan said:

I just installed framework 3.5 so i could play with linq but can not seem to get extension methords working in VS2005.

MS you are a joke with you 3/3.5 framework and many developers think that V3.0 has linlq

# November 19, 2007 4:05 PM

James Kovacs said:

A great new feature of Visual Studio 2008 is multi-targeting, which allows VS 2008 to compile for .NET

# January 15, 2008 1:55 PM
New Comments to this post are disabled
Page view tracker