2014-05-05 56 views
4

我在運行時在C#中加載DLL時出現問題。加載DLL的C#異常。找不到解決方案

以下代碼:

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尋找更多的信息,(來源: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. 
     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 

我嘗試了一些解決方案,但沒有任何工程。 有什麼新鮮的想法?

+0

您是否驗證過PDB文件? –

+0

@MarkHall你的意思是什麼? – krzakov

+0

是說它無法找到與some_dll.dll相關的PDB文件。 –

回答

3

當您在調試模式下構建代碼時,會創建兩個文件,一個是類庫和另一個是「調試數據庫」.pdb文件。如果您在調試模式下運行代碼,則包含pdb文件以及bin你的反射代碼。 另一件事是在命令提示符下使用fuslogvw查看Fusion日誌。它可能會給出任何未被照顧的運行時故障/依賴關係。

+0

那麼,目前我正在調試它在DEBUG模式,如你所預測的。我應該如何添加它以及在哪裏? – krzakov

+0

我檢查了'bin \ Debug \'目錄,並且在這裏看不到任何PDB。 – krzakov

相關問題