簡體   English   中英

加載 DLL 期間的 C# 異常。 找不到解決辦法

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

我在運行時在C#加載DLL時遇到問題。

以下代碼:

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

所以例外說:

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.

我稍微搜索了堆棧並找到了這個catch來尋找更多信息(來源:這里

捕獲塊:

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

所以我設法提取:

'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

我嘗試了一些解決方案,但沒有任何效果。 有什么新鮮的想法嗎?

在調試模式下構建代碼時,會創建兩個文件,一個是類庫,另一個是“調試數據庫”.pdb 文件。如果您在調試模式下運行代碼,請將 pdb 文件也包含在反射代碼的 bin 中. 另一件事是在命令提示符下使用 fuslogvw 查看 Fusion 日志。它可能會提供任何未處理的運行時故障/依賴項。

在發布模式下,我收到了相同的神秘錯誤(“無法加載一個或多個請求的類型。檢索 LoaderExceptions 屬性以獲取更多信息”),因此 amolDotnet 的解決方案不適用於我的情況。 為了偶然發現相同問題的任何其他人的利益,我將指出我設法通過添加 DLL 所需的缺少的 Nuget 包依賴項來修復它。 T 之前在 Nuget 包控制台中關閉了依賴警告,DLL 運行得很好,直到我嘗試加載程序集並通過反射檢查成員。 如果 krzakov 沒有提到對異常信息的深入檢查,我可能找不到這個替代解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM