Skip to content

Instantly share code, notes, and snippets.

@philiplaureano
Created October 28, 2014 22:18
Show Gist options
  • Save philiplaureano/154d0225fbb0e8561cdd to your computer and use it in GitHub Desktop.
Save philiplaureano/154d0225fbb0e8561cdd to your computer and use it in GitHub Desktop.
Yep. If you want app-wide AOP, here's the interface that you need to implement that does the method replacement:
public interface IMethodCallProvider
{
void AddMethodCalls(object target, MethodBase hostMethod, IEnumerable<MethodBase> interceptedMethods,
IMethodCallMap methodCallMap, StackTrace stackTrace);
}
Most of the parameters on the AddMethodCalls method are self explanatory, except for the methodCallMap parameter, which looks like this:
public interface IMethodCallMap
{
bool ContainsMappingFor(MethodBase method);
void Add(MethodBase method, IMethodCall methodCall);
IMethodCall GetMethodCall(MethodBase method);
}
There's also a sample implementation of the IMethodCallProvider called SingleMethodCallProvider, which does the heavy lifting of matching the current method call against the signature of the method that it will be replacing.
At this point, the only thing you need to do is add your method call provider to the global method call registry, which is what the Replace class facade does for you:
private static void AddMethodCall(MethodBase targetMethod, MulticastDelegate implementation)
{
// Verify that the method and the implementation have compatible signatures
var method = implementation.Method;
var hasCompatibleMethodSignature = method.HasCompatibleMethodSignatureWith(targetMethod);
if (!hasCompatibleMethodSignature)
throw new InvalidOperationException(
string.Format(
"The delegate you provided does not have a compatible signature with the '{0}' method.",
targetMethod.Name));
MethodCallProviderRegistry.AddProvider(new SingleMethodCallProvider(targetMethod, implementation));
}
The Replace class facade is just a helper class to add syntactic sugar around interception. If you want something more heavy-duty, then you probably want to implement your own method call provider.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment