.Assembly / GetExportedTypes throws a FileNotFoundException

If I run this code:

var myAsm = typeof(MyType).Assembly; var types = myAsm.GetExportedTypes(); 

I get:

 System.IO.FileNotFoundException : Could not load file or assembly .... 

which lists the dependent assembly. However, if I do this:

 var myAsm = Assembly.LoadFrom(...); // DLL containing the same assembly as above var types = myAsm.GetExportedTypes(); 

It works great.

I would prefer the first technique, since it is cleaner ... why should I load a DLL that is already loaded? Any tips?

+4
source share
3 answers

This doesn’t exactly answer your question, but I had a problem with it, and I thought that I would send some information to help others who might stumble upon this, just like me!

Assembly has

 .LoadFile(string path) 

and

 .LoadFrom(string path) 

LoadFile throw a FileNotFoundException if you load the assembly from some remote (not the same as the executable dll) folder. You should use LoadFrom as you do above;)

+3
source

You tried

 System.Reflection.Assembly.GetExecutingAssembly(); 

or

 System.Reflection.Assembly.GetAssembly(typeof(MyType)); 

The reason your second one works is because you are really downloading the .dll. When you call typeof (MyType) .Assembly, it has no idea which reflection dll should use. This is why GetExecutingAssembly or GetAssembly(tyepof(MyType)) should work.

+1
source

If you are trying to load an assembly that is NOT ExecutingAssembly, and that the DLL has references to lower level objects such as MyProj.Interfaces or MyProj.BaseClasses or Infragistics controls or something like that ... THESE dll will also have to be available in the same folder from which you are loading the initial dll.

0
source

All Articles