tags:

views:

172

answers:

2

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?

+1  A: 

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.

David Basarab
The first one would not give me the right assembly. The second one gave me the same FileNotFoundException.
JoelFan
A: 

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 ;)

Andrew Bullock