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

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

问题描述

我正在查看 msdn 文档,但我仍然对加载程序集时使用 LoadFileLoadFrom 之间究竟有什么区别感到有些困惑.有人可以提供一个例子或类比来更好地描述它.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(assembly1.CodeBase == assembly2.CodeBase);

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

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

<小时>

编辑:要回答您在修改后的问题中提出的问题,您一定要阅读Suzanne Cook 关于装配标识.


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天全站免登陆