的LoadFile和LoadFrom与.NET程序集之间的区别? [英] Difference between LoadFile and LoadFrom with .NET Assemblies?

查看:249
本文介绍了的LoadFile和LoadFrom与.NET程序集之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找在MSDN文档,我仍然到底是什么的LoadFile 两者的区别和 LoadFrom 加载程序集时。有人可以提供一个例子或比喻来更好地描述它。 MSDN文档更糊涂了。此外,为 ReflectionOnlyLoadFrom 一样的 LoadFrom 除了它加载组件仅在反射模式。

I was looking at the msdn documentation and I am still a little confused on what exactly is the difference between using LoadFile and LoadFrom when loading an assembly. Can someone provide an example or an analogy to better describe it. The MSDN documentation confused me more. Also, Is ReflectionOnlyLoadFrom the same as LoadFrom except that it loads the assembly only in reflection mode.

由于我的.NET体验是不是最大的,这里有关于使用的LoadFile MSDN文档一些问题:

Since my .NET experience is not the greatest, here are some questions regarding the MSDN documentation using LoadFile:

1)是什么通过的LoadFile 的意思是检查有相同的标识组件,但位于不同的路径?什么是身份(例如)?

1) What does it mean by LoadFile examines assemblies that have the same Identity, but are located in different paths? What is the identity (example)?

2),它规定了的LoadFile 不将文件加载到LoadFrom上下文并不能解决使用负载路径依赖。这是什么意思,有人可以提供一个例子吗?

2) It states the LoadFile does not load files into the 'LoadFrom Context' and does not resolve dependencies using the load path. What does this mean, can someone provide an example?

3)最后,它指出的LoadFile 就是在这有限的情况下非常有用,因为LoadFrom无法加载具有相同的名称,但是在不同的路径组件;它只会加载第一个这样的组件,而这又使我想到了同样的问题,什么是集身份?

3) Lastly, it states that LoadFile is useful in this limited scenario because LoadFrom cannot load assemblies that have the same identities but different paths; it will only load the first such assembly, which again brings me to the same question, what is the assemblies identity?

推荐答案

这是否清晰起来?

// path1 and path2 point to different copies of the same assembly on disk:

Assembly assembly1 = Assembly.LoadFrom(path1);
Assembly assembly2 = Assembly.LoadFrom(path2);

// These both point to the assembly from path1, so this is true
Console.WriteLine(string.Compare(assembly1.CodeBase, assembly2.CodeBase) == 0);

assembly1 = Assembly.LoadFile(path1);
assembly2 = Assembly.LoadFile(path2);

// These point to different assemblies now, so this is false
Console.WriteLine(string.Compare(assembly1.CodeBase, assembly2.CodeBase) == 0);



修改:回答你的问题,修订提出的问题,你一定要读的Suzanne库克在大会​​身份

Edit: to answer the questions you raised in your revised question, you definitely want to read Suzanne Cook on Assembly Identity.

有很多管理如何组件加载规则,他们中的一些都与他们如何解决依赖关系 - 如果你的AssemblyA依赖于AssemblyB,应该在哪里.NET去找找AssemblyB?在全局程序集缓存,同一个目录下就发现AssemblyA,或其他地方完全?此外,如果发现该程序集的多个副本,究竟应该如何选择使用哪一个?

There are a lot of rules that govern how assemblies are loaded, and some of them have to do with how they resolve dependencies - if your AssemblyA is dependent on AssemblyB, where should .NET look to find AssemblyB? In the Global Assembly Cache, the same directory it found AssemblyA, or somewhere else entirely? Furthermore, if it finds multiple copies of that assembly, how should it choose which one to use?

LoadFrom 有一组规则,而的LoadFile 有另一套规则。这是很难想象的原因有很多使用的LoadFile ,但如果你需要使用同一组件的不同拷贝反映,它的存在给你。

LoadFrom has one set of rules, while LoadFile has another set of rules. It is hard to imagine many reasons to use LoadFile, but if you needed to use reflection on different copies of the same assembly, it's there for you.

这篇关于的LoadFile和LoadFrom与.NET程序集之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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