简体   繁体   English

加载 DLL 期间的 C# 异常。 找不到解决办法

[英]C# Exception during loading DLL's. Can't find solution

I have a problem, while loading DLL at runtime in C# .我在运行时在C#加载DLL时遇到问题。

Following code:以下代码:

foreach (var f in Directory.EnumerateFiles(startFolder, "*.dll",            SearchOption.AllDirectories))
{
   try
   {
       var assembly = Assembly.LoadFrom(f);
       var types = assembly.GetTypes(); //exception raised here

       foreach (var type in types)
       {
            if (type.GetInterface("IPlayerFactory") != null)
                 instances.Add((IPlayerFactory)Activator.CreateInstance(type));
       }
       assembly = null;
    }
    catch (ReflectionTypeLoadException ex) { /* later, keep reading  */}

So exception says:所以例外说:

An exception of type 'System.Reflection.ReflectionTypeLoadException' occurred in
mscorlib.dll but was not handled in user code

Additional information: Unable to load one or more of the requested types. Retrieve the    
LoaderExceptions property for more information.

I searched stack a little bit and found this catch to look for more information (source: here )我稍微搜索了堆栈并找到了这个catch来寻找更多信息(来源:这里

catch block:捕获块:

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.
      Console.WriteLine(sb);
 }

So i managed to extract:所以我设法提取:

'Battleships.Desktop.vshost.exe' (CLR v4.0.30319: Battleships.Desktop.vshost.exe):
 Loaded 'C:\SomePath\some_dll.dll'. Cannot find
 or open the PDB file.
 A first chance exception of type 'System.Reflection.ReflectionTypeLoadException'  
 occurred in mscorlib.dll

I tried some solutions but nothing works.我尝试了一些解决方案,但没有任何效果。 Any fresh ideas?有什么新鲜的想法吗?

When you build your code in debug mode, two files are created,one the class library and other the 'debug database' .pdb file.if you are running your code in debug mode, include pdb file as well in bin of your reflection code.在调试模式下构建代码时,会创建两个文件,一个是类库,另一个是“调试数据库”.pdb 文件。如果您在调试模式下运行代码,请将 pdb 文件也包含在反射代码的 bin 中. Another thing is to look at the Fusion logs using fuslogvw in command prompt.it may give any runtime failures/dependrncies that are not taken care of.另一件事是在命令提示符下使用 fuslogvw 查看 Fusion 日志。它可能会提供任何未处理的运行时故障/依赖项。

I received the same arcane error ("Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information") while in Release mode, so amolDotnet's solution didn't apply in my situation.在发布模式下,我收到了相同的神秘错误(“无法加载一个或多个请求的类型。检索 LoaderExceptions 属性以获取更多信息”),因此 amolDotnet 的解决方案不适用于我的情况。 For the benefit of anyone else who stumbles upon the same problem, I'll point out that I managed to fix it by adding a missing Nuget package dependency that the DLL needed.为了偶然发现相同问题的任何其他人的利益,我将指出我设法通过添加 DLL 所需的缺少的 Nuget 包依赖项来修复它。 T had previously turned off dependency warnings in the Nuget package console and the DLL had functioned just fine, until I tried to load the assembly & inspect the members through Reflection. T 之前在 Nuget 包控制台中关闭了依赖警告,DLL 运行得很好,直到我尝试加载程序集并通过反射检查成员。 I might not have found this alternate solution if krzakov hadn't mentioned a deep inspection of the Exception info.如果 krzakov 没有提到对异常信息的深入检查,我可能找不到这个替代解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM