我无法将自定义dll加载到我的新的 appDomain
.
AppDomainSetup appSetup = new AppDomainSetup()
{
ApplicationName = "PluginsDomain",
ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
PrivateBinPath = @"Plugin",
DisallowBindingRedirects = false,
DisallowCodeDownload = true,
ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
};
AppDomain domain = AppDomain.CreateDomain("MyPlugin", null, appSetup);
byte[] bytes = File.ReadAllBytes(@"C:\Users\testUser\Documents\Visual Studio
2012\Projects\BaseClass\BaseClass\bin\helpClass\HelperClass.dll");
Assembly assm= domain.Load(bytes);
Type type=assm.assm.GetExportedTypes()[0];
Object testObj=(Object)appDomain.CreateInstanceAndUnwrap(assm.FullName, type.FullName);
///Here is the problem When I try to compile it show error.
你需要在你的远程域("MyPlugin")中为AssemblyResolve实现一个事件监听器,以返回所述的汇编(在你的例子中是HelperClass.dll)。
你是想把HelperClass当作 "代理 "使用吗?如果可能的话,我建议你创建一个 "代理"(继承自MarshalByRefObj),然后创建InstanceAndUnwrap到你的远程域。然后在 "Proxy "中实现AssemblyResolve,并通过 "Proxy "加载动态汇编。
//Listenner to AssemblyResolve
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
if(args.Name==assm.Fullname)
{
return assm;
}
else
{
return null;
}
}