【问题标题】:Cannot GetTypes() in new domain在新域中无法 GetTypes()
【发布时间】:2020-11-07 22:49:03
【问题描述】:

我创建了一个新的域,然后将程序集加载到这个域中,但是当 GetTypes() 出现如图所示的错误时,希望大家帮忙,谢谢。 代码

public class Program
{       
    public static void Main()
    {
        string assemblyPath = @"D:\Github\BeyConsPlugin\BeyConsProject\bin\x64\Debug\BeyConsRevitProject.dll";
        AppDomain appDomain = CreateChildDomain(AppDomain.CurrentDomain, Guid.NewGuid().ToString());
        appDomain.AssemblyResolve += AssemblyResolve;
        var value = (Proxy)appDomain.CreateInstanceAndUnwrap(typeof(Proxy).Assembly.FullName, typeof(Proxy).FullName);
        var assembly = value.GetAssembly(assemblyPath);
        var types = assembly.GetTypes();
        Console.ReadKey();
    }

    private static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
    {
        AssemblyName assemblyName = new AssemblyName(args.Name);
        string dependentAssemblyFilename = Path.Combine(@"D:\Github\BeyConsPlugin\BeyConsProject\bin\x64\Debug", assemblyName.Name + ".dll");
        if (File.Exists(dependentAssemblyFilename)) return null;
        return Assembly.LoadFile(dependentAssemblyFilename);
    }

    public static AppDomain CreateChildDomain(AppDomain parentDomain, string domainName)
    {
        Evidence evidence = new Evidence(parentDomain.Evidence);
        AppDomainSetup setup = parentDomain.SetupInformation;
        return AppDomain.CreateDomain(domainName, evidence, setup);
    }
}

public class Proxy : MarshalByRefObject
{
    public Assembly GetAssembly(string assemblyPath)
    {
        try
        {
            return Assembly.LoadFile(assemblyPath);
        }
        catch { return null; }
    }
}

错误

【问题讨论】:

  • 你为什么一直发布基本相同的问题?正如我在您的original 问题中提到的,您使用的Codeproject 代码存在多个问题。另外我看到你没有使用我在answer 中建议的解决路径,你说的那个留下了你的问题“未解决”

标签: c#


【解决方案1】:

您是否检查过 BeyConsRevitProject.dll 程序集是否在应用程序的 bin 目录中?这是一个可能的原因。尝试删除 bin/ 和 obj/ 文件夹并重建您的解决方案,如果错误仍然存​​在,请使用以下代码确定错误的真正原因:

using System.IO;
using System.Reflection;
using System.Text;

try
{
    //The code that causes the error goes here.
}
catch (ReflectionTypeLoadException ex)
{
    StringBuilder sb = new StringBuilder();
    foreach (Exception exSub in ex.LoaderExceptions)
    {
        sb.AppendLine(exSub.Message);
        FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
        if (exFileNotFound != null)
        {                
            if(!string.IsNullOrEmpty(exFileNotFound.FusionLog))
            {
                sb.AppendLine("Fusion Log:");
                sb.AppendLine(exFileNotFound.FusionLog);
            }
        }
        sb.AppendLine();
    }
    string errorMessage = sb.ToString();
    //Display or log the error based on your application.
}

此代码是 Ben Gripka 在此处建议的: Error message 'Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.'

【讨论】:

  • 我删除了 bin 目录并重建了它,按照您的指示使用 try-catch,但它没有跳转到 catch 内。下图
  • 这是因为现在抛出了一个不同的异常,而这个异常不是来自 ReflectionTypeLoadException 类型。但是通过这个新的异常,我们可以确认程序集确实有/有一个缺失的引用。检查项目依赖项上的 BeyConsRevitProject 并写入其正确路径,如果在项目中的任何地方都没有引用它,则将其添加为依赖项
  • 我项目的.csproj:docs.google.com/document/d/…
  • 这个dll真的在你本地机器的这个路径中吗? D:\Github\BeyConsPlugin\BeyConsProject\bin\x64\Debug\BeyConsRevitProject.dll
  • 是的,所有的dll都在那里 D:\Github\BeyConsPlugin\BeyConsProject\bin\x64\Debug
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-26
相关资源
最近更新 更多