Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Assembly / GetExportedTypes throws FileNotFoundException

Tags:

c#

reflection

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 a dependent assembly. However, if I do:

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

it works fine.

I really would prefer the first technique, as it's cleaner... why should I have to load a DLL that is already loaded? Any advice?

like image 957
JoelFan Avatar asked Sep 22 '09 14:09

JoelFan


2 Answers

This doesn't exactly answer your question, but I just had a related problem to this and I thought I'd post some info to help others who may stumble across this as I did!

Assembly has

.LoadFile(string path)

and

.LoadFrom(string path)

LoadFile will throw a FileNotFoundException if loading the assembly from some remote (not the same as the executing dll) folder. You need to use LoadFrom as you do above ;)

like image 105
Andrew Bullock Avatar answered Oct 23 '22 22:10

Andrew Bullock


Have you tried

System.Reflection.Assembly.GetExecutingAssembly();

Or

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

The reason your second one works is you are actually loading a .dll. When you call typeof(MyType).Assembly, it has no idea which .dll reflection should be using. Which is why either GetExecutingAssembly or GetAssembly(tyepof(MyType)) should work.

like image 24
David Basarab Avatar answered Oct 23 '22 22:10

David Basarab