从路径加载装配

编程入门 行业动态 更新时间:2024-10-24 00:16:53
本文介绍了从路径加载装配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要从某个位置而不是从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.

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

You used to be able to do what you want in 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)

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

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

更多推荐

从路径加载装配

本文发布于:2023-11-13 10:36:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584147.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   加载

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!