在大圖中,我嘗試從主域中的dll執行方法,但之後卸載該dll。到目前爲止,我已經創建了新的AppDomain
加載了Assembly
\ dll那裏,用MarshalByRefObject
我提取了該方法的主體和MaxStackSize
到主域,在那裏創建了DynamicMethod
,重新創建了它內部的主體並調用它。 但是,當我調用它時,我得到異常: System.BadImageFormatException: Signature is not IMAGE_CEE_CS_CALLCONV_LOCAL_SIG
。從另一個AppDomain的程序集複製方法,並從CurrentDomain執行它
,其獲取並調用它的代碼:
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
是Proxy類型的對象。 的一些注意事項,可能是必要的:
- ,我取\調用方法是
public static void Run();
。 - 試圖調用方法的
AppDomain
和加載dll \ assembly的AppDomain
具有相同的引用。
編輯
我與第一個字節從GetSignature
方法改變爲0x07
固定簽名。 但還有新的問題System.InvalidProgramException: Common Language Runtime detected an invalid program.
你不能在其他appdomain中調用該方法的任何原因?你不能只是撕掉一些代碼。誰知道它取決於什麼?它肯定依賴於包含程序集的元數據表中的確切標記值。 – usr
@usr我需要它,因爲代碼可能會創建新對象,我需要在卸載AppDomain後保留它們。它不依賴於該dll內的任何內容。只是在參考。我試圖重新創建的代碼是:'Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);' – KugBuBu
它抱怨你使用GetMethodSigHelper()。 CallingConvention.Standard僅適用於使用__stdcall約定的非託管代碼。不知道什麼樣的風格應該看起來像,我一定會先嚐試DynamicMethod.CreateDelegate()。 –