posted on Sunday, September 18, 2005 6:31 PM by anoras

C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies

The PDC is over and the big buzz seems to be around C# 3.0, and Linq in particular. Linq relies on a number of new features in C# 3.0 such as implicitly typed local variables, extension method, lambda functions and anonymous types. A key enabler for the Linq project is the support for generics in the CLR 2.0, and in theory you can achieve the same powerful features in C# 2.0, but you would have to write much more code to do so. The expressive query support in C# 3.0 is in a sense syntactic sugar, meaning that the compiler generates code to support the new features.

The example below shows how you can declare an anonymous type in C# 3.0
var p1=new {Name="Arthur Dent", FavoriteNumber=42 };

This is analogous with writing the following C# 1.0 code
public class AnonynousClass
{
      private string name;
      public string Name
      {
            get { return name; }
            set { name = value; }
      }
      private int favoriteNumber;
      public int FavoriteNumber
      {
            get { return favoriteNumber; }
            set { favoriteNumber = value; }
      }
}
...

AnonynousClass obj=new AnonynousClass();
obj.Name="Arthur Dent";
obj.FavoriteNumber=42; 


Infact, the code created by the C# 3.0 compiler resembles the code above. The only differences are the name of the anonymous type, that the anonymous type is a generic class (so that it can be used within a projection query) and that it overrides the method inherited from System.Object. The most important difference is a non-technical one, namely the amount of code needed to declare and use the type. Another thing to make note of is that in C# 3.0 you can use the new var keyword to have the compiler infer the type of a variable based on the expression used to initialize it.

Extension methods is the most important new feature for Linq to be useful. Extension methods are static methods in a static class with the new this modifier on the first parameter. When you import a class using the using keyword, the extension methods become available on all types thru instance method syntax. The example below show how you can declare a static class with extension methods, and import this class to extend other types you use.

public class Program
{
      static void Main(string[] args)
      {
            var s="Hello World";
            Console.WriteLine(s.WhoAmI());
      }
} 

public static class MyExtensions
{
      public static string WhoAmI(this object obj)
      {
            return obj.GetType().Name;
      }
} 


Many of the new features in C# 3.0 are well-known to developers with experience from dynamic languages such as Python or Ruby. The latter has support for mix-ins, which is a feature that resembles C# 3.0 extension methods. In Ruby you can define a mix-in on the module level. Modules in Ruby cannot include instance methods because they are not classes, but you can import modules within a class definition using the include statement. The example below shows a Ruby implementation of the previous C# 3.0 snippet.

def whoAmI?
     "#{self.type.name}"
end

s = "Hello World"
s.whoAmI?

As with C# 3.0’s var keyword, there is no need to explicitly declare the variable s as a string. Ruby will infer the type based on what you assign to it. A major difference between the two languages is that in C# type inference is done at compile-time so you cannot assign another type, such as an integer, to s after it has been declared. In Ruby type inference is done at runtime. Therefore you can do the following in a Ruby application:

s = "Hello World"
! Outputs “String”
s.whoAmI?
s=42

! Outputs “Fixnum”
s.whoAmI?

Extension methods is an elegant feature, and since you’re able to import functionally into types, it pretty much eliminates the need for multiple-inheritance.

Unfortunately, C# 3.0 won’t be generally available until Visual Studio Orcas is released. Still you have some options to achieve the same powerful feature today within both C# 1.0 and C# 2.0. As with all other new C# 3.0 features, extension methods is macro transformation that is carried out by the compiler. The compiled code for the extension method example looks like this:

[Extension]
public static class MyExtensions
{
      [Extension]
      public static string WhoAmI(object obj);
}
...
string s = "Hello World";
Console.WriteLine(MyExtensions.WhoAmI(text1));

As you can see, a call is made to the static WhoAmI method and the string s is passed as an argument. Delegating calls to a static method adds a lot of complexity to your code, and is by far as elegant as the original C# 3.0 code. If you’re not looking to extend any of the built-in types, such as System.String or System.Int32, there is another way of mimicking extension methods in both C# 1.0 and C# 2.0. You can wrap the type with a dynamic proxy. The following example shows how you can use the Castle project's Dynamic Proxy to wrap a Person class and mix-in a WhoAmI method.

class Program
{
      static void Main(string[] args)
      {
            ProxyGenerator generator=new ProxyGenerator();
            GeneratorContext context = new GeneratorContext();
            WhoAmIMixin whoAmIMixin = new WhoAmIMixin();
            context.AddMixinInstance(whoAmIMixin);
            Person myPerson=(Person) generator.CreateCustomClassProxy(typeof(Person),new WhoAmIInterceptor(),context);
            myPerson.Name=”Arthur Dent”;
            myPerson.FavoriteNumber=42;
            Console.WriteLine(((IWhoAmI)myPerson).WhoAmI());
      }
}

public class WhoAmIMixin : IWhoAmI
{
      private Type actualType;
      public Type ActualType
      {
            get { return actualType; }
            set { actualType = value; }
      }
      public string WhoAmI()
      {
            return actualType.Name;
      }
}

public interface IWhoAmI
{
      Type ActualType
      {
            get;
            set;
      }
      string WhoAmI();
}

public class WhoAmIInterceptor : StandardInterceptor
{
      protected override void PreProceed(IInvocation invocation,  params object[] args)
      {
            IWhoAmI whoAmIImpl=invocation.InvocationTarget as IWhoAmI;
            if (whoAmIImpl!=null)
            {
                  whoAmIImpl.ActualType=invocation.Proxy.GetType().BaseType;
            }
            base.PreProceed(invocation,args);
      }
}

public class Person
{
      private string name;
      public virtual string Name
      {
            get { return name; }
            set { name = value; }
      }
      private int favoriteNumber;
      public virtual int FavoriteNumber
      {
            get { return favoriteNumber; }
            set { favoriteNumber = value; }
      }
}
 

The code is much more verbose than the C# 3.0 extension method or the Ruby mix-in, but it does the same thing. The only difference to a developer programming against your Person type is that she has to cast it to the IWhoAmI interface to gain access to the WhoAmI method defined in it. While it is more cumbersome to do this in the C# versions generally available today, it is possible to extend objects with mix-ins. In a large solution there can be much to gain by applying this technique, and now that the C# 3.0 cat is out of the bag, you know what’s coming up and can align your design towards the new and elegant features that are due in a year or two.

Comments

# download free porn @ Tuesday, May 22, 2007 4:51 AM

<a href= http://honda-civic.moy.su/ >free porn</a>

free porn

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Tuesday, May 29, 2007 10:21 AM

Kim Possible *** http://xxx.hotcartoonimages.com/KimPossible-***/index.html
crest sexy pics http://griffin.toonsbest.com/naruto-sexy-pics.html
Fred nude http://lara.hotcartoonimages.com/Frednude/index.html
trail longer sakura naruto http://crazytoonsfree.com/sakura-naruto-sexy.html
dirty picture simpsons http://teen.freecomicscartoons.com/dirty-picture-simpsons.html
swollen Sleeping else fully porn http://hotcrazytoons.com/Sleeping-Beauty-porn.html
hentai ino naruto http://naruto.crazytoonsfree.com/hentai-ino-naruto.html
time moment dirty http://sexy.crazytoonsfree.com/dirty-lisa.html
Naruto *** http://dagwood.freetoonsbest.com/Narutoslut.html

fosmoco

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Friday, June 08, 2007 11:44 AM

<a href="http://www.rina.coolguide.net/profile/contact_me.php"> coolguide.net </a> <a href="http://www.cedarcreekironworks.com/xcart/product.php?productid=16144"> cedarcreekironworks.com </a> [url=http://ambienz.forumlivre.com/]cheap ambien online[/url] Excellent site, added to favorites.

cheap ambien

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Saturday, June 09, 2007 3:30 PM

<a href="http://www.benchwarmer.de/gaestebuch/index.html"> benchwarmer.de </a> <a href="http://www.beisiegel-castro.de/gaestebuch/index.html"> beisiegel-castro.de </a> http://www.tuoblog.it/adipex/">http://www.tuoblog.it/adipex/ buy adipex [url]http://www.tuoblog.it/adipex/">http://www.tuoblog.it/adipex/[/url] hello, very nice site! please also visit my homepages

buy adipex

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Saturday, June 09, 2007 7:02 PM

You have a great site. <a href="http://www.4x4sdgm.de/gaestebuch/index.html"> 4x4sdgm.de </a> <a href="http://www.4--webmaster.com/gaestebuch/gaestebuch/index.php?page=1"> 4--webmaster.com </a> http://ambien1.forospace.com/forum.php">http://ambien1.forospace.com/forum.php cheapest ambien [url]http://ambien1.forospace.com/forum.php">http://ambien1.forospace.com/forum.php[/url]

cheapest ambien

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Wednesday, June 13, 2007 4:26 AM

Cool!

Skyros

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Thursday, June 14, 2007 1:15 AM

Cool.

Leonidas

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Thursday, June 14, 2007 4:53 AM

Nice...

Demetrios

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Thursday, June 14, 2007 9:50 AM

interesting

Kharilaos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Thursday, June 14, 2007 3:01 PM

Nice...

Myron

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Thursday, June 14, 2007 4:23 PM

Nice!

Epameinondas

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Thursday, June 14, 2007 7:02 PM

Interesting...

Yioryios

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Thursday, June 14, 2007 7:10 PM

Cool!

Chrysostomos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Friday, June 15, 2007 7:27 AM

Nice...

Apostolis

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Friday, June 15, 2007 10:23 AM

interesting

Hristos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Friday, June 15, 2007 2:17 PM

Cool!

Ahmed

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Friday, June 15, 2007 11:34 PM

Interesting...

Alexiou

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Saturday, June 16, 2007 2:43 AM

Nice

Andreas

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Saturday, June 16, 2007 6:21 PM

Nice...

Thanasis

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Sunday, June 17, 2007 12:51 AM

Cool!

Gustas

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Sunday, June 17, 2007 10:50 AM

Cool...

Silvanos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Sunday, June 17, 2007 7:33 PM

Nice...

Marko

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Sunday, June 17, 2007 10:17 PM

Interesting...

Demosthenes

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Monday, June 18, 2007 8:31 AM

Cool...

Agias

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Monday, June 18, 2007 1:35 PM

Sorry :(

Doxiadis

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Tuesday, June 19, 2007 11:14 AM

Nice...

Zacharias

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Tuesday, June 19, 2007 11:47 AM

Nice

Epameinondas

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Tuesday, June 19, 2007 3:08 PM

Interesting...

Christos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Tuesday, June 19, 2007 6:09 PM

Cool...

Kostantinos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Wednesday, June 20, 2007 7:10 AM

Nice...

Samaras

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Thursday, June 21, 2007 1:21 AM

Nice...

Nicolas

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Thursday, June 21, 2007 7:58 PM

Interesting...

Leontios

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Thursday, June 21, 2007 8:52 PM

interesting

Philippos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Friday, June 22, 2007 10:54 AM

Cool.

Kosmas

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Saturday, June 23, 2007 8:14 AM

Nice

Gregorios

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Sunday, June 24, 2007 3:17 AM

Sorry :(

Georghios

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Monday, June 25, 2007 5:55 AM

Cool.

Iason

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Monday, June 25, 2007 1:41 PM

Nice!

Antonios

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Monday, June 25, 2007 4:14 PM

Sorry :(

Konstandinos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Tuesday, June 26, 2007 1:57 AM

interesting

Arsenios

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Tuesday, June 26, 2007 8:09 AM

Nice...

Metrophanes

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Wednesday, June 27, 2007 1:15 AM

Interesting...

Matthaios

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Friday, June 29, 2007 9:49 PM

Nice

Georgios

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Friday, June 29, 2007 11:39 PM

Cool...

Kleanthe

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Saturday, June 30, 2007 12:53 PM

Sorry :(

Efstathios

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Saturday, June 30, 2007 1:55 PM

interesting

Eleftherios

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Saturday, June 30, 2007 3:07 PM

Cool!

Kosta

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Saturday, June 30, 2007 9:40 PM

Cool...

Glafkos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Sunday, July 01, 2007 12:22 AM

Cool...

Aristides

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Sunday, July 01, 2007 1:21 AM

Nice!

Sotirios

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Sunday, July 01, 2007 4:11 AM

Sorry :(

Theologos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Sunday, July 01, 2007 11:54 AM

Nice...

Kyriakos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Sunday, July 01, 2007 7:24 PM

Cool!

Nicolaon

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Tuesday, July 03, 2007 1:11 PM

interesting

Angelo

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Tuesday, July 03, 2007 2:44 PM

Cool!

Damianos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Tuesday, July 03, 2007 10:35 PM

interesting

Giorgos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Wednesday, July 04, 2007 1:29 AM

Interesting...

Markos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Wednesday, July 04, 2007 3:29 AM

Cool!

Manos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Wednesday, July 04, 2007 9:35 AM

interesting

Sotirios

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Wednesday, July 04, 2007 2:17 PM

Nice!

Aiakos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Thursday, July 05, 2007 3:56 PM

Cool...

Anaklets

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Monday, July 09, 2007 6:02 AM

Nice

Manolis

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Monday, July 09, 2007 3:19 PM

Nice...

Dion

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Monday, July 09, 2007 11:34 PM

Sorry :(

Epaminondas

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Tuesday, July 10, 2007 2:55 AM

Interesting...

Spiro

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Tuesday, July 10, 2007 4:21 AM

Nice

Giorgos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Tuesday, July 10, 2007 9:28 AM

Cool.

Konstantinos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Tuesday, July 10, 2007 3:54 PM

Interesting...

Alexis

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Tuesday, July 10, 2007 6:44 PM

Nice

Panagiotis

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Wednesday, July 11, 2007 2:12 AM

interesting

Spiridon

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Wednesday, July 11, 2007 3:16 AM

Interesting...

Dionyssios

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Wednesday, July 11, 2007 4:52 AM

Cool.

Philippos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Wednesday, July 11, 2007 6:57 AM

Nice...

Dionyssios

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Wednesday, July 11, 2007 9:16 PM

Interesting...

Kimon

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Wednesday, July 11, 2007 9:26 PM

Nice!

Philippos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Wednesday, July 11, 2007 9:57 PM

Sorry :(

Yiorgos

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Thursday, July 12, 2007 2:21 AM

Nice...

Vassilis

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Thursday, July 12, 2007 5:44 AM

Nice...

Zacharias

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Thursday, July 12, 2007 6:03 AM

Nice...

Theodosios

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Thursday, July 12, 2007 9:17 AM

Cool!

Ioannis

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Thursday, July 12, 2007 4:42 PM

Sorry :(

Aristotelis

# re: C# 3.0 Extension Methods, Ruby Mixins and Dynamic Proxies @ Thursday, July 12, 2007 5:53 PM

Nice