我在運行時在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
我嘗試了一些解決方案,但沒有任何工程。 有什麼新鮮的想法?
您是否驗證過PDB文件? –
@MarkHall你的意思是什麼? – krzakov
是說它無法找到與some_dll.dll相關的PDB文件。 –