c# - Can OpCodes.Jmp be called to transfer control to a method that resides on a different assembly? -
can method1 resides in assemblya issue opcodes.jmp method1 resides in assemblyb? both methods have same exact signature.
i can't seem make work, getting system.invalidprogramexception : common language runtime detected invalid program.
if redirection inside same assembly, works.
if possible, please provide example using reflection.emit.
you must have missed something. both methods static? have same calling convention?
the following code doesn't reproduce issue:
static void main(string[] args) { var assembly = appdomain.currentdomain.definedynamicassembly (new assemblyname("testassembly"), assemblybuilderaccess.run); var module = assembly.definedynamicmodule("main"); var type = module.definetype("test"); var method = type.definemethod ( "test", methodattributes.public | methodattributes.static, typeof(int), new[] { typeof(string) } ); var gen = method.getilgenerator(); gen.emit(opcodes.jmp, typeof(class1).getmethod("test")); var obj = activator.createinstance(type.createtype()); var func = (func<string, int>) obj.gettype().getmethod("test").createdelegate(typeof(func<string, int>)); var result = func("banana"); console.writeline(result); console.readline(); } and in different assembly, test class:
public static class class1 { public static int test(string hi) { return 42; } } did make sure you're not violating of restrictions?
- the evaluation stack must empty when instruction executed.
- the calling convention, number , type of arguments @ destination address must match of current method.
- the jmp instruction cannot used transferred control out of try, filter, catch, or block.
Comments
Post a Comment