1

I am getting a ReflectionTypeLoadException from the following code :

var myAssembly = Assembly.LoadFrom(myDLL);    
var types = myAssembly.GetTypes())

myDLL references a class in another file in an other assembly ("myDependency.dll")
I made sure that this file resides in the application directory and also in the same folder as "myDLL" file.

How to be able to load myDLL properly in this case ?

Mehdi LAMRANI
  • 11,289
  • 14
  • 88
  • 130
  • Maybe loading the unresolved assemblies in `AppDomain.CurrentDomain.AssemblyResolve` event can help – L.B Jun 08 '12 at 20:18
  • Are you sure the exception is caused by the reference to external dll? – Kendall Frey Jun 08 '12 at 20:18
  • It may be caused by an out of date dll. http://stackoverflow.com/a/7162415/785745 – Kendall Frey Jun 08 '12 at 20:19
  • @KendallFrey : Yes, I made a few safety checks : First, When the reference to the dependency is suppressed before recompiling myDLL, I get no error. Second, I deleted all myDependency.dll & myDLL files and rebuilt & cleaned project, making sure evertything was fresh & @ the right place – Mehdi LAMRANI Jun 08 '12 at 20:48

3 Answers3

1

I'd try calling:

AssemblyName[] referenced = myAssembly.GetReferencedAssemblies();

then iterate over the AssemblyName objects and attempt to load those, prior to calling myAssembly.GetTypes()

ossek
  • 1,648
  • 17
  • 25
  • Well.. As weird as it may sound : A debug/watch of AssemblyName[] referenced shows indeed "myDependency"... I dont' know what to think I am puzzled. Especially that I retried and took off the reference (interface implementation actually) of myDependency from my DLL and it worked fine then. Just to make sure that this is indeed the problem causing the issue. :-/ – Mehdi LAMRANI Jun 08 '12 at 20:57
  • 1
    How would that be possible to get the exception "Could not load file or assembly 'myDependency, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified." while I can see it in the .GetReferencedAssemblies() output ???? – Mehdi LAMRANI Jun 08 '12 at 21:05
  • Plus, I was able to Load the presumably Faulted Dependency DLL from the assemblyName by iterating on myAssembly.GetReferencedAssemblies() output, just like you said. It just blow on the GetTypes(). So strange... – Mehdi LAMRANI Jun 08 '12 at 21:28
  • You might be able to call AssemblyName.CodeBase getter on myDependency to see which location it is being found at, and then see if that location falls under the Load Contexts described here http://msdn.microsoft.com/en-us/library/dd153782.aspx. This page seems to suggest you could try Assembly.Load for default context rather than LoadFrom which includes locations not searched by loader – ossek Jun 08 '12 at 21:34
  • Thank you a lot for your support, it helped me understand a couple'o'things. I finally found a workaround by tweaking the assembly references (see my own answer) – Mehdi LAMRANI Jun 08 '12 at 22:08
1
  1. Make sure all your assemblies (dlls and exes included referenced assemblies) are up to date (platform, configuration and version/build date) and in your exe's directory.

    Try deleting all the projects' OBJ and BIN directories prior to your build to help validate this.

  2. Make sure you do not have other versions in the GAC or a directory in the %PATH%.

Community
  • 1
  • 1
Danny Varod
  • 17,324
  • 5
  • 69
  • 111
0

Well I am proud to announce that I have spoiled my Friday night on some stupid "eye-persistence" phenomenon caused by Visual Studio 2010 (I guess). I Just Copied-Pasted my DependencyDLL project into a brand new assembly/project, deleted the old one, updated old reference to point to the new DependencyDLLNew, And Voilà : That did the trick. Problem solved. Thank to those who helped.

Mehdi LAMRANI
  • 11,289
  • 14
  • 88
  • 130
  • I should've hung out with colleagues to the pub when I was offered to. "Naaah just a small bug to resolve and I'm off. Won't take long". It's past Midnight now. ***SIGH*** – Mehdi LAMRANI Jun 08 '12 at 22:06