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?