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

Is it possible for DynamicMethod to call (via ILGenerator.EmitCall - or a similar one), for example, a "normal" method, for example. Private Sub BlahBlah (ByVal obj as Object)?

Thank you in advance

+4
source share
2 answers
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(); } 
+4
source

Upload values ​​to the evaluation stack that must be assigned to the method

 MethodInfo methodInfo = typeof(ClassName).GetMethod(MethodName, new Type[1] { typeof(-method argument types-) }); IL.Emit(OpCodes.Call, methodInfo ); 
+4
source

All Articles