Thursday, November 02, 2006

DuckTyping for existing classes

First off, if you don't lurk on CodeProject, I mock you... but assuming you either don't, or just missed this one, Günter Prossliner posted a great bit of code that lets you dynamically "implement" any interface against a class instance by at runtime building a shim class that forwards all the calls through to matching methods in the target.

This is what is commonly called DuckTyping, and allows you to program "as if" an object implements any arbitrary interface, even if it really doesn't inherit from that class. In my Dynamic library, I let you do that for a single method at a time. Günter's code lets you do it an interface at a time, which is obviously cooler if you have multiple methods that you expect a class to implement (and for which you can define an interface).

I think my next weekend project will be to port his code, which uses dynamic assemblies, to use my Dynamic library instead (and thus benefit from LCG).

1 comment:

Anonymous said...

Implementing duckTyping via your library would be really cool.. I would just wondering if it would be possible to like customize the post/pre access code.. something like dynamic interceptors (see codeproject).. ??