从MethodInfo生成DynamicMethod

时间:2010-06-23 18:24:24

标签: c# reflection dynamic methods

我正在阅读Joel Pobar关于Reflection的Dodge Common Performance Pitfalls to Craft Speedy Applications文章,我正在查看一段未编译的特定代码(略微修改以缩小到特定错误,因为他的示例有更多错误):

MethodInfo writeLine = typeof(Console).GetMethod("WriteLine");
RuntimeMethodHandle myMethodHandle = writeLine.MethodHandle;
DynamicMethod dm = new DynamicMethod(
    "HelloWorld",          // name of the method
    typeof(void),          // return type of the method
    new Type[]{},          // argument types for the method
    false);                // skip JIT visibility checks

ILGenerator il = dm.GetILGenerator();
il.Emit(OpCodes.Ldstr, "Hello, world");
il.Emit(OpCodes.Call, myMethodHandle); // <-- 2 errors here
il.Emit(OpCodes.Ret);

错误是:

Program.cs(350,13): error CS1502: The best overloaded method match for 'System.Reflection.Emit.ILGenerator.Emit(System.Reflection.Emit.OpCode, byte)' has some invalid arguments
Program.cs(350,35): error CS1503: Argument '2': cannot convert from 'System.RuntimeMethodHandle' to 'byte'

ILGenerator可以Emit MethodInfo,但它似乎不支持MethodHandle ...是否有人知道如何使此示例正常工作?

2 个答案:

答案 0 :(得分:3)

喜欢这样吗?

        MethodInfo writeLine = typeof(Console).GetMethod("WriteLine", new Type[] {typeof(string)});
        RuntimeMethodHandle myMethodHandle = writeLine.MethodHandle;
        DynamicMethod dm = new DynamicMethod(
            "HelloWorld",          // name of the method
            typeof(void),          // return type of the method
            new Type[] { },          // argument types for the method
            false);                // skip JIT visibility checks

        ILGenerator il = dm.GetILGenerator();
        il.Emit(OpCodes.Ldstr, "Hello, world");
        il.EmitCall(OpCodes.Call, writeLine, null);
        il.Emit(OpCodes.Ret);
        // test it 
        Action act = (Action)dm.CreateDelegate(typeof(Action));
        act();

的变化:

  • 我使用经过调整的GetMethod来查找(string)重载(否则这是一个模棱两可的匹配)
  • 使用MethodInfo,而不是句柄(因为那是ILGenerator想要的)
  • 使用EmitCall(其他可能也有效,但我知道这种方式有效)

答案 1 :(得分:0)

通过Nuget.org上的这个库: ClassWrapper

它为内部使用动态生成的方法的类型创建包装器。所以没有使用反射(只能在ClassWrapperDescriptor.Load方法中生成动态表达式)

鉴于,例如e类名为SampleClass

//Create the class wrapper descriptor, this can be cached and reused!
var classWrapperDescriptor = new ClassWrapperDescriptor(typeof(SampleClass));
//Initialize the descriptor
classWrapperDescriptor.Load();

//Create the instance of our object
object instance = new SampleClass();

//Create an instance of the wrapper
var classWrapper = classWrapperDescriptor.CreateWrapper(instance);

//Set a property
classWrapper.Set("StringProperty","test");

//Get a property
var stringPropertyValue = classWrapper.Get<string>("StringProperty");

//Invoke a method without return statement
classWrapper.Invoke("ParamMethod", "paramValue");

//Invoke a method witho return statement
var result = classWrapper.Invoke<int>("ReturnMethod", "paramValue");

非常感谢有关此图书馆的任何反馈!