I'm making a Portable Class Library (PCL) in .NET and it happens that, when trying to abstract any behavior, I face the very common annoyance that .NET Framework is very possessive with its types and interfaces. It's very usual to find a type doesn't implement any interface, or when it does, the interface is internal.
When existing types have compatible methods (same name and signature) it's rather easy: I have been using ImpromptuInterface like this:
nakedInstanceTheDoesNotImplementAnything.ActAs<MyBeautifulInterface>();
and I get exactly what I want. Transparent and handy.
But, what to do when some methods are slightly different?
- Different names
- Different call site: one is a property getter and the other is a method
- Some methods that are different, but easily adaptable between them with minor modifications.
Normally, a pure OOP approach is recommended and we are told to create and Adapter. But when you have to adapt a complex hierarchy of types, this can be really tedious and complex as well, even more when you have HUGE classes like UIElement, Control, FrameworkElement…
The question is: Can I make ImpromptuInterface overcome those variations in the types to dynamically create adapters?
So
ImpromtuInterface
uses the DLR, basically when you call ActLike() it emits and caches a proxy for that interface, and wraps it around your object.Since it's a dynamic invocation, you don't actually have the method on your target if it's and IDynamicMetaObjectProvider the most popular to customize being
System.Dynamic.DynamicObject
.But this is a lot of work, because it you have to handle the non modified calls as much as the modified calls.
There are several prefab
DynamicObject
s inImpromptuInterface
that I've moved to a separate library Dynamitey.One in particular,
BaseForwarder
sounds like what you want, because rather than handing all the logic, forwarding the message to a target object is already implemented as the base functionality.Then to use it would be
new DynamicAdapter(myObject).ActLike<IMyInterface>()