我正在從一個新的AppDomain中的特定「擴展」目錄加載所有DLL,以獲取與這些相關的一些反射信息。接收來自另一個AppDomain的程序集引用的異常
這就是我想:
我創造了我的解決方案一個新的圖書館AssemblyProxy
這只是有這個類:
public class AssemblyProxy : MarshalByRefObject
{
public Assembly LoadFile(string assemblyPath)
{
try
{
return Assembly.LoadFile(assemblyPath);
}
catch
{
return null;
}
}
}
我要確保這個DLL出現在我的「擴展」目錄。然後,我使用下面的代碼將「extensions」目錄下的所有程序集加載到新的AppDomain
中。
foreach(string extensionFile in Directory.GetFiles(ExtensionsDirectory, "*.dll"))
{
Type type = typeof(AssemblyProxy.AssemblyProxy);
var value = (AssemblyProxy.AssemblyProxy) Domain.CreateInstanceAndUnwrap(
type.Assembly.FullName,
type.FullName);
var extensionAssembly = value.LoadFile(extensionFile);
types.AddRange(extensionAssembly.GetTypes());
}
某些DLL確實會加載成功,但在某些DLL異常被拋出這樣的:
Could not load file or assembly 'Drivers, Version=2.3.0.77, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
編輯:唯一的例外是不是在新的AppDomain拋出。 DLL成功加載到新的AppDomain中。只要程序集引用返回到主/調用AppDomain,就會拋出異常。主/調用AppDomain是否嘗試在接收引用時自行加載程序集?
謝謝。
你試過了什麼? –
是否[[ReflectionOoadOnly'](http://msdn.microsoft.com/zh-cn/library/ms172331(v = vs.100).aspx)足以滿足您的(未知)目的? –
[CreateInstanceAndUnwrap](http://msdn.microsoft.com/en-us/library/system.appdomain.createinstancefromandunwrap.aspx)有什麼問題? – Algirdas