My question is:
If I am going to create a DynamicMethod object corresponding to ConstructorInfo.Invoke , what types of IL do I need to implement to handle all (or most) types of arguments, when can I guarantee that the correct type and number of arguments will be passed before I call?
Background
I am in the third iteration of my IoC container and am currently profiling to find out if there are any areas where I can easily shave off a lot of time.
One thing I noticed is that when resolving a particular type, I end up with a calling constructor using ConstructorInfo.Invoke , passing in an array of arguments that I developed.
What I noticed is that the invoke method has quite a bit of overhead, and I wonder if these are just different implementations of the same checks as me.
For example, due to the constructor matching code, I have to find a matching constructor for the predefined parameter names, types and values that I went through, there is no way that this particular call would cause something like this to be able to handle, like the correct number of arguments, in the correct order, of the correct type and with the corresponding values.
, , DynamicMethod, , :
- ConstructorInfo.Invoke: 1973ms
- DynamicMethod: 93ms
20% . , ConstructorInfo.Invoke DynamicMethod, , 20% factory -scoped (.. ).
, , DynamicMethod .
, , ConstructorInfo, .
, , IL:
l001: ldarg.0 ; the object array containing the arguments
l002: ldc.i4.0 ; the index of the first argument
l003: ldelem.ref ; get the value of the first argument
l004: castclass T ; cast to the right type of argument (only if not "Object")
(repeat l001-l004 for all parameters, l004 only for non-Object types,
varying l002 constant from 0 and up for each index)
l005: newobj ci ; call the constructor
l006: ret
- , ?
, , , , " " ( ), , , .