首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MyAppDomain AssemblyResolve C#

MyAppDomain AssemblyResolve C#
EN

Stack Overflow用户
提问于 2015-01-28 21:58:16
回答 1查看 754关注 0票数 0

我有一个DLL,我在需要自己处理外部程序集负载的环境中使用它。我要将程序集加载到AppDomain中。当我在CurrentAppDomain中尝试这一点时,它工作得很好,但在我自己创建的应用程序域中执行这一点时失败了。背景是,我想在最后卸载appdomain,这样程序集最终就会“释放”。

代码语言:javascript
复制
public ZipEx() 
{
    try
    {
        AppDomainSetup domaininfo = new AppDomainSetup();
        domaininfo.ApplicationBase = System.Environment.CurrentDirectory;
        Evidence adevidence = AppDomain.CurrentDomain.Evidence;

        //THIS CODE WORKS
        //System.AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
        //System.AppDomain.CurrentDomain.Load("ICSharpCode.SharpZipLib");

        //THIS CODE DOES NOT WORK
        AppDomain zipDomain2 = AppDomain.CreateDomain("ADZib2", adevidence, domaininfo);
        PolicyLevel polLevel = PolicyLevel.CreateAppDomainLevel();
        PermissionSet permSet = new PermissionSet(PermissionState.Unrestricted);
        permSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.AllFlags));
        polLevel.RootCodeGroup.PolicyStatement = new PolicyStatement(permSet);
        zipDomain2.SetAppDomainPolicy(polLevel);
        zipDomain2.AssemblyResolve += CurrentDomain_AssemblyResolve;
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show("ex in ctor" + Environment.NewLine + ex.ToString());
    }
    if (_loadedAssembly == null)
    {
    }
}

Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    System.Windows.Forms.MessageBox.Show("CurrentDomain_AssemblyResolve");
    Assembly assembly = null;
    bool foundAssembly = false;

    int idx = args.Name.IndexOf(',');
    if (idx > 0)
    {
        string partialName = args.Name.Substring(0, idx);
        string dllName = partialName + ".dll";

        //Add the directorys where the assembly hould be resolved
        List<string> directorySearch = new List<string>
        {
          string.Format("{0}{1}{2}",Environment.CurrentDirectory,Path.DirectorySeparatorChar, dllName),
          string.Format("{0}{1}{2}",AppPath,Path.DirectorySeparatorChar, dllName)
        };

        foreach (string fileName in directorySearch)
        {
            if (File.Exists(fileName))
            {
                foundAssembly = true;
                assembly = Assembly.LoadFrom(fileName);
                break;
            }
        }

        if (assembly == null)
        {
            if (!foundAssembly)
            {
                foreach (string fileName in directorySearch)
                {
                }
            }
            else
            {
            }
        }
    }
    if (assembly != null) 
    {
        System.Windows.Forms.MessageBox.Show("assembly is not null");
    }
    return assembly;
}

我的问题是如何使用我创建的appdomain来加载程序集?

EN

回答 1

Stack Overflow用户

发布于 2015-01-28 23:00:34

在您当前的示例中,我没有看到任何未注释的代码会导致新创建的AppDomain尝试加载程序集。我假设在这个答案中有一个对Load的调用,这个调用在您的未注释代码中丢失了。

根据AppDomain.Load帮助中的注释,它应该只在当前的AppDomain中使用,否则它将尝试将程序集加载到两个AppDomains中,这可能是您收到异常的原因。若要仅将程序集加载到另一个AppDomain中,应调用其中一个CreateInstance函数。在这种情况下,我推荐使用CreateInstanceFromAndUnwrap函数,因为它允许您按名称和类型指定程序集。

如果您没有要实例化并能够跨AppDomain边界进行交互的类型(这似乎不太可能,但却是可能的),您可能必须创建并丢弃一些简单的类型,如枚举或结构,才能使其工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28194081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档