Again on Expression Tree vs Reflection
In last post Expression Tree vs Reflection I showed how to use Expression Tree to dynamically build a function object at runtime that you can use to invoke methods of unknown objects. This is a peculiar use of Expression Tree, but you can obtain the same result with Lightweight Code Generation (as some people commented in the post). Moreover the LCG approach is probably faster because the expression tree uses LCG in the Compile method, so if you directly generate your code in IL, there is no need of intermediate Expression Tree object. (You have also full control on IL). Remember that when I say that LCG is faster, I’m speaking about the time needed to build the dynamic function, and not the real time needed to invoke the function. To compare the two techniques here is an example, I want to be able to write code like this.
|
|
I have a type that is unknown at compile time, I know that it has a method called AMethod, and I want to create a Func<Object, Int32> object to dynamically invoke that method instead of invoking it with plane reflection. Here it is a possible implementation of ReflectFunction with Expression Tree:
|
|
It is really straightforward to write, first of all get the MethodInfo object for the method you want to invoke, then create a parameter of type Object, then a conversion parameter to cast the object to the correct type (line 11) and finally the expression representing the call to the method. To obtain a Func<Object, T> object I need to create a lambda expression, cast it to the appropriate type, and finally call its Compile method. With LCG the code to obtain the same result is the following.
|
|
The code is not complex, but we need to know IL to make it works. After I grab the MethodInfo object I create a DynamicMethod with the constructor that accepts the name of the method, the return type and the list of parameters types. Then you can call the GetILGenerator method, and you will obtain a ILGenerator object that you can use to write IL code into the new method body. The IL code is really simple, since the dynamic method will be generated as is a static method (check the documentation to see how to dynamically create an instance method) the first argument is the object passed to the function, so I use Ldarg_0 to push on the stack, then a Castclass to be sure that the caller passed an object of the correct type, and finally the Callvirt to actually invoke the right method, now the return value is on the stack, so you can call Ret. The final touch is calling CreateDelegate method of the DynamicMethod specifying the signature of the function, and the game is done.
For those interested in performance, here is a simple test
|
|
And the corresponding output.
Reflection = 0.892017037086468 Expression Tree 0.012591549690529 LCG 0.0127834533958392
As you can see the Expression Tree method seems to be a little faster, but the timing is quite the same. The real thing to notice is that both techniques are really faster than invoking the method through the MethodInfo object.
alk.
Tags: .NET Framework Lightweight Code Generation Expression Tree