开发者

How to call a 'normal' method through ILGenerator.Emit*?

开发者 https://www.devze.com 2023-03-03 13:57 出处:网络
Is it possible for a DynamicMethod to 开发者_C百科call (via ILGenerator.EmitCall -- or similar -- for instance) a \'normal\' method, e.g. Private Sub BlahBlah(ByVal obj as Object)?

Is it possible for a DynamicMethod to 开发者_C百科call (via ILGenerator.EmitCall -- or similar -- for instance) a 'normal' method, e.g. Private Sub BlahBlah(ByVal obj as Object)?

Thanks in advance


Load values on evaluation stack to be given to the method

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

IL.Emit(OpCodes.Call, methodInfo );


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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号