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?
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 ;)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With