4

DynamicMethod 是否可以调用(例如通过 ILGenerator.EmitCall 或类似方法)“正常”方法,例如 Private Sub BlahBlah(ByVal obj as Object)?

提前致谢

4

2 回答 2

4

在评估堆栈上加载值以提供给方法

MethodInfo methodInfo = typeof(ClassName).GetMethod(MethodName, new Type[1] { typeof(-method argument types-) });

IL.Emit(OpCodes.Call, methodInfo );
于 2011-07-23T06:47:47.150 回答
4
delegate void foo();

public static void show(string foo)
{
    MessageBox.Show(foo);
}

public void test()
{
    DynamicMethod dm = new DynamicMethod("foo", null, null);
    ILGenerator gen = dm.GetILGenerator();
    gen.Emit(OpCodes.Ldstr, "hello world");
    gen.EmitCall(OpCodes.Call, this.GetType().GetMethod("show"),null);
    gen.Emit(OpCodes.Ret);
    var b = dm.CreateDelegate(typeof(foo)) as foo;
    b();
}
于 2011-06-15T21:54:44.443 回答