1

我无法将自定义 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.

4

1 回答 1

0

您将需要在您的远程域(“MyPlugin”)中为 AssemblyResolve 实现一个事件侦听器,该侦听器返回所述程序集(在您的情况下为 HelperClass.dll)

您是否尝试将 HelperClass 用作“代理”?如果可能的话,我建议您创建一个“代理”(从 MarshalByRefObj 继承),然后 CreateInstanceAndUnwrap 到您的远程域。然后在“代理”中实现 AssemblyResolve 并通过“代理”加载动态程序集。

//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;
    }
}

我之前写过一篇关于 AppDomain 和实现“代理”的帖子

于 2013-08-12T14:16:31.260 回答