.NET Reflection Extravanganza !!!
.NET Reflection Extravanganza !!!
I was involved in this module for the past few weeks and successfully completed it in a very innovative way. Ok, this was the problem. We have a COM server, let us name it Server. I had to write an assembly in C#, let us call it Bridge, that will listen to all of the events fired by the Server and perform an action Action. To keep that Action simple, let us assume we have to log them in the database. But the Server fires hundreds of events, and it is not wise to write up static
event handlers for all of them. Also if more events are added in the future, it is possible to support the Bridge.
So I came up with a different approach with the incredible Reflection in .NET. All of the events fired by the Server, its prototype and other relevant information can be got through reflection, and for each of the event methods, an event sink [event handler] can be generated at runtime. This means I have to create a method at runtime matching the prototype of the event. The dynamic method thus generated at runtime has to appended with some method body so as to do the Action, and then has to be registered as the event sink for the corresponding event. So when the event is fired by the Server, the dynamically created event handler is called without any intervention. This is the theme of my solution. This keeps the Bridge unaffected for any event related changes in the Server.
But achieving this solution and making it work was a great and exciting adventure.
- I was able to get the event information about the events fired by Server through reflection. I used the following sort of code for generating the dynamic method or supposedly the dynamic event handler:
using System; using System.Threading; using System.Reflection; using System.Reflection.Emit; class TaskDynamicEventHandler { public static void CreateDynamicEventHandler(ref TypeBuilder myTypeBld, string methodName, Type[] eventMethodParameters, Type eventreturnType) { MethodBuilder myMthdBld = myTypeBld.DefineMethod(methodName, MethodAttributes.Public | MethodAttributes.Static, eventreturnType, eventMethodParameters); ILGenerator ILout = myMthdBld.GetILGenerator(); int numParams = eventMethodParameters.Length; for (byte x = 0; x < numParams; x++) { // Load the parameter onto the evaluation stack ILout.Emit(OpCodes.Ldarg_S, x); } // Use the above sort of logic to access the event parameter // values and then package into a hashtable, and then call // a static method HandleEvent in TaskDynamicEventHandler, // which takes the hashtable as a parameter. All the code is // generated in IL using ILGenerator. ILout.Emit(OpCodes.Ret); } public static void Main() { AppDomain myDomain = Thread.GetDomain(); AssemblyName asmName = new AssemblyName("DynamicAssembly1"); AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave); ModuleBuilder myModule = myAsmBuilder.DefineDynamicModule("DynamicModule1", "MyDynamicAsm.dll"); TypeBuilder myTypeBld = myModule.DefineType("MyDynamicType", TypeAttributes.Public); string dynamicMethodName = "DynamicEventHandler"; CreateDynamicEventHandler(myTypeBld, dynamicMethodName, eventMethodParameters, eventreturnType); Type myType = myTypeBld.CreateType(); myAsmBuilder.Save("MyDynamicAsm.dll"); } }
The drawback in this approach was that a dynamic assembly+module+type was getting created for each event. This was not efficient enough, and so slightly altered the logic to create the dynamic assembly+module+type once and add the methods [dynamic event handlers] to the dynamic type.
Though a level of efficiency may have been achieved, it was not elegant enough to be satisfied. The dynamic event handlers [DEH] are all methods of a specific type belonging to a different assembly that is generated at run-time, and these DEH do not belong to the same assembly as the
TaskDynamicEventHandler
class. The responsibility of the DEHs was to read its parameter name and values at runtime, package them into a hashtable and call a methodHandleEvent
ofTaskDynamicEventHandler
, and it is inHandleEvent
that the actual job of logging is done. Well, the actual job is not only logging but other things that require access to the members ofTaskDynamicEventHandler
. So the non-elegance here was thatHandleEvent
was exposed aspublic static
method so that the DEH in the dynamic assembly could call, which lead to the ugliness whereHandleEvent
was exposed to the outside world from the assembly to whichTaskDynamicEventHandler
belongs to. SoHandleEvent
cannot be non-public. But it was required for other reasons to be an instance method. - Here is the most interesting part. The aim was then to make the DEH call an instance method of the
TaskDynamicEventHandler
i.e.HandleEvent
. How do you make astatic
method call aninstance
method ? Well, if I have an object reference of theTaskDynamicEventHandler
class in the DEH execution, and IF I can load that on to the evaluation stack [using the IL code/ILGenerator
], then I call an instance method. That was the pain and it was pretty tricky and interesting that everybody to whomever I explained could not correctly grasp that the 'this
' used during the compiled code will not be the same in the runtime IL code of the DEH, and neither can I load an object reference without me creating it or getting it as a parameter. That is all the .NET type security. You will not be given any chance to doreinterpret_cast
kind of stuff at all. But you can pass theTaskDynamicEventHandler
object reference [this
] to the DEH but that beats the goal where the prototype of the DEH will not match the prototype of its corresponding event and so cannot act as a sink. - Here comes .NET 2.0 to the rescue to a certain extent, and helps us achieve the aim - Efficiency and Elegance. There is a class by name
DynamicMethod
to dynamically create methods. The beauty of theDynamicMethod
is that it is possible to add the dynamic method as a member of the current classTaskDynamicEventHandler
.// returnType - event method's return type // parameterTypes - event method's parameter types list ArrayList parameterTypes = new ArrayList(parameterTypes); parameterTypes.Insert(0, this.GetType()); DynamicMethod dynamicEventHandler = new DynamicMethod(methodName, returnType, (Type[])parameterTypes.ToArray(typeof(Type)), typeof(TaskDynamicEventHandler));
Hence the DEH is now a part of the same assembly and class and it can call even non-public methods. Efficiency was well achieved but still elegance was a few feet away. The dynamic method created and added to
TaskDynamicEventHandler
usingDynamicMethod
class is astatic
method and hence cannot accessinstance
methods ofTaskDynamicEventHandler
, although it can access non-public methods. - Here is the most interesting part. The aim of this iteration is to make the DEH call an instance method of the
TaskDynamicEventHandler
i.e.HandleEvent
. How do you make astatic
method call an instance method ? Well, if I have an object reference of theTaskDynamicEventHandler
class during the DEH execution, and if I can load that on to the evaluation stack [using the IL code/ILGenerator
], then I call an instance method. Things are going to get interesting now. The difference between aninstance
andstatic
method is that aninstance
method has the object reference, to which it belongs, as the first parameter while astatic
method does not. Though syntactically, the object reference is not added, the compiler adds it. So while creating the dynamic method [DEH] for an event, theTaskDynamicEventHandler
object reference [this
] is added as the first parameter to the event parameter list. This makes the DEH seem to be aninstance
method. So during runtime, when an event is fired, its corresponding DEH executes, theLgarg_0
in the IL code represents the object reference it belongs to, and it is the same as that forHandleEvent
. - But even now the
HandleEvent
ispublic
and is vulnerable for improper usage. I made it a virtual method. That is fun, and now it is entirely the user's responsibility to avoid improper usage, and it is upto the user to overrideHandleEvent
to do whatever he wants. - Few minor things- Added
Trace.WriteLine
in the IL code usingILGenerator
for debugging; Addedtry
-catch
exception blocks for catching exceptions, but unfortunately does not seem to work.
All of these approaches until the final efficient and elegant solution took several iterations of revisit and review. I will not able to explain about the difficulties and tough IL debugging experience that I went through trying to make the HandleEvent
an instance virtual method, although I will be able to share the joy and knowledge now. It was a great experience !!!