从路径加载装配 [英] Load assembly from Path

查看:78
本文介绍了从路径加载装配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从某个位置而不是从GAC加载程序集.我无法从GAC中删除该程序集,因为我的程序必须在不同的计算机上运行,​​而我无法控制该程序集是否在GAC中.

I need to load an assembly from a certain location and not from the GAC. I can not remove the assembly from the GAC, because my program has to run on different machines, where I couldn't control if the assembly is in the GAC or not.

我在项目中添加了对DLL文件的引用.不幸的是,该程序集是从GAC加载的.

I added a reference to the DLL-File to my project. Unfortuntely the assembly is loaded from the GAC.

我在Startup-事件

var directory = Thread.GetDomain().BaseDirectory;
var dllPath = Path.Combine(directory, "MyAssembly.dll");
var assembly = Assembly.LoadFrom(dllPath);

不幸的是,程序集仍从GAC加载.

Unfortunately the assembly is still loaded from the GAC.

即使程序集在GAC中,我也应该如何从给定位置加载它?

What can I do to load it from the given location, even if the assembly is in GAC?

推荐答案

不幸的是,您将无法绕过GAC-Assembly.LoadFrom()Assembly.LoadFile(),甚至无法将其加载到字节数组中并使用都将首先检查GAC是否具有相同标识的程序集,然后加载它.

Unfortunately, you won't be able to bypass the GAC - Assembly.LoadFrom(), Assembly.LoadFile(), and even loading it into a byte array and using Assembly.Load(byte[]) will all check the GAC first for an assembly with the same identity, and load it instead.

过去能够在.net 1.1中执行所需的操作,但是从2.0开始,将首先检查GAC. 请参阅运行时如何定位程序集-请注意,步骤3是在探查目录之前检查GAC(即使您已完全指定它)

You used to be able to do what you want in .net 1.1, but since 2.0, the GAC is checked first. See How the Runtime Locates Assemblies - note that step 3 is check the GAC, before probing directories (even if you you fully specified it)

如果要为反射加载一个(检查程序集,而不是运行它),则可以使用

If you want to load one up for Reflection (examining the assembly, rather than running it), you can use Assembly.ReflectionOnlyLoadFrom()

这篇关于从路径加载装配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆