7

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?

Community
  • 1
  • 1
krzakov
  • 3,871
  • 11
  • 37
  • 52
  • Have you verified that the PDB file is there? – Mark Hall May 05 '14 at 02:03
  • @MarkHall what do You mean exactly? – krzakov May 05 '14 at 02:04
  • Is says it can't find the PDB file associated with some_dll.dll – Mark Hall May 05 '14 at 02:05
  • I have no idea about any PDBfile. I'm including dll file alone, without any external files. I checked directory, not any PDB. – krzakov May 05 '14 at 02:08
  • Side note: you may want to open the DLL in question with ILDasm or ILSpy and see if there are some dependencies obviously missing... If you really need to get all types when some assemblies with base classes/interface are missing you'll need to resort to reading metadata directly... – Alexei Levenkov May 05 '14 at 02:13

2 Answers2

6

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.

amolDotnet
  • 139
  • 6
1

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.

SQLServerSteve
  • 332
  • 1
  • 11
  • 22