\$\begingroup\$

Sometimes when working with components created by a library vendor such as DevExpress you want to extend the components. Good vendors will leave plenty of virtual methods for you to put to use, but once in a while you need to hook into an interface method call in a class that implements the method explicitly. You can simply reimplement the interface method, but nine times out of ten you will still want to call the base implementation as though it were virtual. As a matter of fact, it should have been virtual (CA1033).

In the meantime, the base implementation is private and you need to use reflection. This class mitigates some of that nuisance so that you don't have to hand-roll reflection code. It hands you an object implementing the interface that calls directly into the private base implementation methods.

An example is worth a thousand explanations:

interface IExampleInterface { string InterfaceMethod(); } class BaseClass : IExampleInterface { // The authors of BaseClass did not follow CA1033: Interface methods should be callable by child types string IExampleInterface.InterfaceMethod() { return "BaseClass implementation"; } } sealed class CustomizedClass : BaseClass, IExampleInterface { // We have no choice but to override by implementing the method. string IExampleInterface.InterfaceMethod() { // Now in order to access the base explicit implementation, we use the class below: return LanguageUtils.ExplicitImplementation<BaseClass, IExampleInterface>(this).InterfaceMethod() + " has been customized."; } }

Surprisingly, there's barely any code: