AsImplementationOf since it's an extension method on
object and too likely to collide.
A while back I was talking about Interface Segregation and proposed using either LinFu's DynamicObject or delegate injection. While I played a bit more with delegate injection, in practical use delegate injection turned out to be rather ugly and not really improve readability.
So, I've come back to wanting to cast an object to an interface regardless of what interfaces that object implemented. I wanted this to be as simple and lightweight as possible, so rather than using a dynamic proxy framework, i simply rolled my own IL and wrote a pure proxy that does nothing but call the identical method on the class it wraps.
DuckPond is a very simple and focused library. It currently adds only a single extension method:
Given a class
Duck that implements a number of methods, including
we can easily cast
Duck to a more limited interface that the class doesn't implement such as:
That's all there is to it.
Honestly, i don't know yet. I have not benchmarked the generated classes against virtual method dispatches or LinFu's and Castle's dynamic proxy. I assume it is, since unlike with dyanmic proxy, DuckPond doesn't use an interceptor. Instead it emits Intermediate Language for each call in the interface, dispatching the call against the wrapped instance's counterpart.
The code is available now at GitHub: http://github.com/sdether/duckpond