我在运行时在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 */}
所以异常(exception)说:
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
我尝试了一些解决方案,但没有任何效果。
有什么新主意吗?
最佳答案
在 Debug模式下构建代码时,将创建两个文件,一个是类库,另一个是“调试数据库” .pdb文件。如果在 Debug模式下运行代码,则在反射代码的bin中也应包含pdb文件。
另一件事是在命令提示符下使用fuslogvw查看Fusion日志。它可能会给出任何 Unresolved 运行时故障/依赖关系。
关于加载DLL时发生C#异常。找不到解决方案,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23463711/