Question

I have a problem, while loading DLL at runtime in C#.

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 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?

Was it helpful?

Solution

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. 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.

OTHER TIPS

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. 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. 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. I might not have found this alternate solution if krzakov hadn't mentioned a deep inspection of the Exception info.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top