patterncsharpMinor
Facility to run code explicitly implemented in a base class that does not follow CA1033
Viewed 0 times
facilitynotimplementedca1033explicitlythatfollowdoescodeclass
Problem
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:
Surprisingly, there's barely any code:
```
public static class LanguageUtils
{
public static TInterface ExplicitImplementation(TBase @this)
where TBase : TInterface
where TInterface : class
{
return (TInterface)new ExplicitImplementationProxy(typeof(TBase), @this).GetTransparentProxy();
}
private sealed class ExplicitImplementationProxy : RealProxy, IRemotingTypeInfo
{
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(this).InterfaceMethod() + " has been customized.";
}
}Surprisingly, there's barely any code:
```
public static class LanguageUtils
{
public static TInterface ExplicitImplementation(TBase @this)
where TBase : TInterface
where TInterface : class
{
return (TInterface)new ExplicitImplementationProxy(typeof(TBase), @this).GetTransparentProxy();
}
private sealed class ExplicitImplementationProxy : RealProxy, IRemotingTypeInfo
{
Solution
This is a pretty tidy implementation, not much to say here. A couple very minors:
-
This is a bit hard to read:
I'd split it up:
-
Clone the argument array instead of explicitly copying it:
Expresses the semantics on a more compact way.
-
For me this seems to be some sort of method forwarding so I'd possibly consider renaming it and maybe turning it into an extension method (I like the syntactic sugar of it). Something along these lines:
In which case the call would then read as:
-
This is a bit hard to read:
return new ReturnMessage(.Invoke(instance, args), args, args.Length, methodCall.LogicalCallContext, methodCall);I'd split it up:
var targetMethod = map.TargetMethods[Array.IndexOf(map.InterfaceMethods, (MethodInfo)methodCall.MethodBase)];
return new ReturnMessage(targetMethod.Invoke(instance, args), args, args.Length, methodCall.LogicalCallContext, methodCall);-
Clone the argument array instead of explicitly copying it:
var args = (object[])methodCall.Args.Clone();Expresses the semantics on a more compact way.
-
For me this seems to be some sort of method forwarding so I'd possibly consider renaming it and maybe turning it into an extension method (I like the syntactic sugar of it). Something along these lines:
public static TInterface ForwardToBase(this TBase @this)
where TBase : TInterface
where TInterface : class
{
return (TInterface)new ExplicitImplementationProxy(typeof(TBase), @this).GetTransparentProxy();
}In which case the call would then read as:
this.ForwardToBase().InterfaceMethod()Code Snippets
return new ReturnMessage(.Invoke(instance, args), args, args.Length, methodCall.LogicalCallContext, methodCall);var targetMethod = map.TargetMethods[Array.IndexOf(map.InterfaceMethods, (MethodInfo)methodCall.MethodBase)];
return new ReturnMessage(targetMethod.Invoke(instance, args), args, args.Length, methodCall.LogicalCallContext, methodCall);var args = (object[])methodCall.Args.Clone();public static TInterface ForwardToBase<TBase, TInterface>(this TBase @this)
where TBase : TInterface
where TInterface : class
{
return (TInterface)new ExplicitImplementationProxy(typeof(TBase), @this).GetTransparentProxy();
}this.ForwardToBase<BaseClass, IExampleInterface>().InterfaceMethod()Context
StackExchange Code Review Q#93976, answer score: 2
Revisions (0)
No revisions yet.