Load strongly-name assembly from specific path?

我只是一个虾纸丫 提交于 2020-01-02 17:16:14

问题


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;


来源:https://stackoverflow.com/questions/3103858/load-strongly-name-assembly-from-specific-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!