And then came Mono. I was never too interested in the Mono project, as I never envisioned writing .NET programs for any platform other than Windows, but anything to shut those damn "write-once run-anywhere" java geeks up was good for the .NET community. While looking over AOP frameworks a couple of weeks ago, I ran across a link to the Mono.Cecil project. After reading about Mono.Cecil, I discovered that this was exactly that for which I had been searching. This framework allows you to inject CIL (the new MSIL) directly into compiled assemblies. I wrote a framework using Mono.Cecil called Dynamic Aspect Linking Engine (dale for short) that injects the code like the following examples:
Assume we have the following code:
public static void PublicMethodTest (string testParam1, int testParam2)
{
// do something interesting here
}
After running the injecter, the code becomes:
public static void PublicMethodTest (string testParam1, int testParam2)
{
System.Reflection.MethodBase _dale_method = (new System.Diagnostics.StackFrame(0)).GetMethod();
dalelib.AspectManager.EnterMethod(method, new object[] { testParam1, testParam2 });
try
{
// do something interesting here
}
catch (Exception exception)
{
dalelib.AspectManager.MethodError(_dale_method, exception);
throw;
}
finally
{
dalelib.AspectManager.ExitMethod(_dale_method);
}
}
The AspectManager class creates any defined aspects (defined by attributes) and notifies them of the entering, exiting, and/or exception.
The only caveat with injecting code with Cecil is that you must know CIL. The MSDN documentation on CIL is fairly extensive, but if you are more comfortable dragging and dropping components than you are writing them from scratch, then you may want to wait for a commercial product that does the work for you. I am seriously considering packaging the Dynamic Aspect Linking Engine up and either selling it or posting the source code on the internet, so check back if you are interested.