[英]Load assembly to temporary Appdomain from another directory
作為BaaS項目的一部分,我需要從“guest”程序集動態執行代碼。 我已經嘗試了其他類似問題的所有示例和答案,包括AppDomainToolKit方法,但沒有運氣。
與插件方法類似,我通過將相關程序集復制到主機應用程序的bin路徑中獲得了積極的結果。 但是,對於當前場景,這是不可能的:
到目前為止,我的最新一段代碼如下
// ClassLibrary1.dll and ClassLibrary2.dll are in the same directory, both are marked as Serializable
var binPath = @"C:\AssemblyDemo\ClassLibrary1\bin\Debug\";
var lib1Path = binPath + "ClassLibrary1.dll";
var lib2Path = binPath + "ClassLibrary2.dll";
var setup = new AppDomainSetup();
setup.ApplicationBase = binPath;
AppDomain domain = AppDomain.CreateDomain("domainname", null, setup);
ObjectHandle handle = domain.CreateInstanceFrom(lib1Path, "ClassLibrary1.Class1");
var unwrap = handle.Unwrap();
var m1 = unwrap.GetType().GetMethod("Method1");
var result = m1.Invoke(unwrap, null);
handle.Unwrap()
拋出異常Type is not resolved for member 'ClassLibrary1.Class1,ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
如果只想在臨時AppDomain中加載程序集,則無法在主域中調用Unwrap
。 Unwrap
也在那里加載類型。 您必須將對象的所有訪問權限從臨時程序集移動到臨時AppDomain。 您可以使用AppDomain.DoCallBack
或您在子域中實例化並從父域調用的自定義MarshalByRef
類來執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.