Welcome to MSDN Blogs Sign in | Join | Help

jaredpar's WebLog

Code, rants and ramblings of a programmer.

Syndication

News

Now Reading

Expert F#

What's a better book to read when learning F#?

Essential WPF

Thus far the best book I've read on WPF. Gets right down to working with WPF and the goals/history.

Purely Functional Data Structures

Reading this book makes me feel like I'm back in college. It will really get your mind going and is best read with a whiteboard handy.

Blog Roll

Eric Lippert
Dustin Campbell
Jon Skeet
Coding Horror
Brian McNamara
Hub FS
Full List

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

Published Friday, November 16, 2007 3:37 PM by Jared Parsons

Filed under: ,

Comments

# MSDN Blog Postings » Extension Methods without 3.5 Framework @ Friday, November 16, 2007 5:16 PM

PingBack from http://msdnrss.thecoderblogs.com/2007/11/16/extension-methods-without-35-framework/

MSDN Blog Postings » Extension Methods without 3.5 Framework

# re: Extension Methods without 3.5 Framework @ Friday, November 16, 2007 5:50 PM

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?

Adrian Anttila

# re: Extension Methods without 3.5 Framework @ Friday, November 16, 2007 6:00 PM

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)

       {

       }

   }

}

Jared Parsons

# re: Extension Methods without 3.5 Framework @ Friday, November 16, 2007 6:29 PM

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.

Adrian Anttila

# re: Extension Methods without 3.5 Framework @ Friday, November 16, 2007 6:29 PM

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.

Adrian Anttila

# re: Extension Methods without 3.5 Framework @ Friday, November 16, 2007 6:30 PM

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

Tony

# re: Extension Methods without 3.5 Framework @ Friday, November 16, 2007 6:36 PM

This trick will only work with VS2008.  

Jared Parsons

# 在 .NET Framework x (x @ Saturday, November 17, 2007 9:19 AM

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

Adrian H.

# re: Extension Methods without 3.5 Framework @ Monday, November 19, 2007 4:05 PM

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

alan

# Syntactic Sugar, Compiler Candy, and Other Sweets @ Tuesday, January 15, 2008 1:55 PM

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

James Kovacs

New Comments to this post are disabled
Page view tracker