c# - Copy method from another AppDomain's assembly and execute it from CurrentDomain -
in big image trying execute method dll in main domain after that, unload dll. far i've created new appdomain loaded assembly\dll there, marshalbyrefobject fetched body , maxstacksize of method main domain, created dynamicmethod there, recreated body within , invoked it. when invoke exception: system.badimageformatexception: signature not image_cee_cs_callconv_local_sig.
the code fetches , invokes it:
dynamicmethod method = new dynamicmethod( "func", typeof( void ), new type[ 0 ] ); var info = method.getdynamicilinfo( ); info.setcode( marshal.getilcode( ), marshal.getmaxstacksize()); info.setlocalsignature( signaturehelper.getmethodsighelper( callingconventions.standard, typeof( void ) ).getsignature( ) ); method.invoke( null, new object[ 0 ] ); //<-- exception here marshal object of type proxy. notes might necessary:
public static void run();. the appdomain trying invoke method , appdomain loads dll\assembly have same references. edit
i fixed signature changing first byte 0x07 getsignature method. there's new problem system.invalidprogramexception: mutual language runtime detected invalid program.
your implementation has big fault, byte array methodbase.getmethodbody is not portable.
a method invocation using opcodes.call or opcodes.callvirt results in several bytes, 1 indicating type of phone call (call/callvirt) followed 4 bytes represents metadata token. token can, in case, parsed int32 , resolved methodbase using module.resolvemethod.
note these metadata tokens generated @ compile time; may differ between compilations. (while it's implementation detail of compiler, believe auto-incremented numbers based on order of methods seen during compilation.)
this means actual bytes you're trying utilize contains metadata tokens invalid in dynamic module. may point existing method different signature you're expecting, or may not nowadays @ in module.
you need parse il-code , emit again, generating new , valid metadata tokens.
c# reflection.emit
No comments:
Post a Comment