简体   繁体   中英

Can't do Assembly.Load(String) with a referenced assembly unless I instantiate a class within that assembly first. How to solve?

I have a very strange problem here. It looks like unless I instantiate a class within an assembly I get an assembly not found error.

For example:

Assembly.Load("something.blah, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")

Type mqType = Type.GetType(query.Attribute(fullyQualifiedName + ", " + assemblyInfo);
Object mq = Activator.CreateInstance(mqType);

Throws a FileNotFound exception on Assembly.Load

This:

Assembly.Load("something.blah, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")

new someClassInAssembly();

Type mqType = Type.GetType(query.Attribute(fullyQualifiedName + ", " + assemblyInfo);
Object mq = Activator.CreateInstance(mqType);

Works fine. Yes, even if it is instantiated after Assembly.Load, so it is clearly a problem during compilation. How do I explicitly make sure that the assembly is loaded and findable during runtime, is there a compilation setting somewhere, what do I need to do?

Make sure you're loading the assembly you think you're loading, by supplying the path:

AssemblyName an = AssemblyName.GetAssemblyName(filePath);
Assembly.Load(an);

Honestly, if its just a single reference or a handful, just add an explicit reference somewhere it will save you a lot of effort.

//Use a static constructor somewhere appropriate. 
static someClass(){
   new AssemblyYouCareAbout.Object();
}

The alternatives are along the lines of hauling dlls manually to the bin of your running process or to add the dlls to the gac. I'd rather use the not-so-elegant static constructor and move on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM