Geeks With Blogs

News This is the *old* blog. The new one is at blog.sixeyed.com
Elton Stoneman
This is the *old* blog. The new one is at blog.sixeyed.com

[Source: http://geekswithblogs.net/EltonStoneman]

David Meyer has a neat duck typing library for .NET which I’ve been looking at as I had a requirement to treat objects of various types in the same way. They all expose a “Value” property, but don’t implement an interface.

The library does more than I needed so I put together a quick duck typing implementation which just lets me treat an instance of an object which tacitly implements an interface, as a true implementation of the interface. In this example I can access BespokeDateTime and BespokeInt as IHasValue:

Using Castle’s Dynamic Proxy, this is simple to do – just requiring you to generate an interface proxy with a custom interceptor. The interceptor invokes the interface method on the real object, so you effectively wrap an instance in an interface it doesn’t explicitly implement:

    BespokeDateTime bespoke = new BespokeDateTime(new DateTime(2000, 1, 1));

    IHasValue duck = DuckType.As<IHasValue>(bespoke);

    Console.WriteLine(duck.Value);

- or using an object extension to hide the implementation:

    bespoke.As<IHasValue>().Value;

Iterating over collections is much cleaner with duck typing – if I have a collection that contains both BespokeDateTime and BespokeInt objects, to get all their values I can use:

    foreach (object obj in GetObjects())

    {

        Assert.IsNotNull(DuckType.As<IHasValue>(obj).Value);

    }

- compared to casting each object:

    foreach (object obj in GetObjects())

    {

        BespokeDateTime bespokeDateTime = obj as BespokeDateTime;

        if (bespokeDateTime != null)

        {

            Assert.IsNotNull(bespokeDateTime.Value);

        }

        else

        {

            BespokeInt bespokeInt = (BespokeInt) obj;

            Assert.IsNotNull(bespokeInt.Value);

        }

    }

Use with caution over large collections, as there is a noticeable performance hit in using the duck type. Over 500,000 items the duck-typed code takes 40-50 times as long as the casting code, averaging 8 seconds to 0.2 seconds, with duck-typing by extension taking marginally longer again:

 


 

The full code (all 34 lines – including validation that the type to duck does tacitly implement the interface) is on MSDN Code Gallery here: Duck Typing with Castle.

 

Posted on Wednesday, October 21, 2009 2:59 AM Code Gallery | Back to top


Comments on this post: Duck Typing with Castle

# re: Duck Typing with Castle
Requesting Gravatar...
Brilliant. I ported to Silverlight 3 if anyone wants the bits. These bits of insight that are simple but deeply insightful are what I love most in our work as software craftsman. Fantastic insight into much more than just 'an API' is obvious in this, and your implementation shows you go deep and wide in both.
Left by Damon Wilder Carr on Oct 24, 2009 3:49 PM

# re: Duck Typing with Castle
Requesting Gravatar...
Thank you for this brilliant post. I really needed to know this. I will be browsing your site for more great coding tips.
Left by buy backlinks on Mar 25, 2010 9:02 PM

# re: Duck Typing with Castle
Requesting Gravatar...
Nice! I've never thought about it. It works!
Left by Bleed Radiator on Apr 27, 2010 6:23 AM

# re: Duck Typing with Castle
Requesting Gravatar...
This is really a brilliant post. I really needed to know these tips. Thank you. I will be browsing your site for more useful tips.
Left by data backup solutions on Jan 25, 2011 9:05 AM

# re: Duck Typing with Castle
Requesting Gravatar...
great website and a good post with plently of tips

Thanks
Left by remortgage advice on Apr 29, 2011 11:17 PM

# re: Duck Typing with Castle
Requesting Gravatar...
So it's written in C#, how can I convert it to VB, thank you
Left by i4Tips on May 07, 2011 4:51 AM

# re: Duck Typing with Castle
Requesting Gravatar...
This is one of the most incredible blogs Ive read in a very long time. The amount of information in here is stunning, like you practically wrote the book on the subject. Your blog is great for anyone who wants to understand this subject more. Great stuff; please keep it up!
Left by elite London escorts on May 19, 2011 5:40 AM

Your comment:
 (will show your gravatar)


Copyright © Elton Stoneman | Powered by: GeeksWithBlogs.net