-->

Load strongly-name assembly from specific path?

2019-05-25 06:25发布

问题:

I have a strongly-named assembly, installed to a specific folder (and not the GAC).

The name as shown in Reflector is:

"Foo.Bar.TreeFrog, Version=1.2.1.0, Culture=neutral, PublicKeyToken=ac88c4a8b22089b4"

and the path where it's installed is

"c:\\QueueBall"

Can I use Assembly.Load or Assembly.LoadFrom to load it, and if so how?

Can I ensure that the strong naming is honored, i.e. that the DLL I'm loading really is the one I'm expecting and not an imposter with the same name?

回答1:

You could use LoadFrom:

var assembly = Assembly.LoadFrom(@"c:\QueueBall\Foo.Bar.TreeFrog.dll");

Note that this will also load referenced assemblies into the application domain running this code. If you don't want this behavior you could use the LoadFile method.


UPDATE:

You can check the assembly identity before loading it to make sure that it has not been tampered with:

AssemblyName an = AssemblyName.GetAssemblyName(@"c:\QueueBall\Foo.Bar.TreeFrog.dll");
byte[] key = an.GetPublicKey();
Version version = an.Version;