需要澄清:.NET 运行时如何解析来自父文件夹的程序集引用?
我的解决方案中有以下可执行文件的输出结构:
%ProgramFiles% | +-[MyAppName] | +-[Client] | | | +-(EXE & several DLL assemblies) | +-[Common] | | | +-[Schema Assemblies] | | | | | +-(several DLL assemblies) | | | +-(several DLL assemblies) | +-[Server] | +-(EXE & several DLL assemblies)
解决方案中的每个项目都引用不同的 DLL 程序集,其中一些是解决方案中其他项目的输出,其他是普通的第 3 方程序集。 例如,[Client] EXE 可能引用 [Common] 中的程序集,该程序集位于不同的目录分支中。
所有引用都将“复制本地”设置为 false,以镜像最终安装的应用程序中的文件布局。
现在,如果我查看 Visual Studio IDE 中的引用属性,我会发现每个引用的“路径”都是绝对的,并且它对应于程序集的实际输出位置。 这是可以理解和正确的。 正如预期的那样,解决方案编译并运行得很好。
我不明白的是,为什么即使我关闭 IDE、重命名 [MyAppName] 目录并手动运行 [Client] EXE,一切似乎都能正常工作? 如果引用路径与链接时不同,运行时如何找到程序集?
需要明确的是 - 这实际上正是我所追求的:一组半分散的应用程序文件,无论 [MyAppName] 目录位于何处,甚至其名称是什么,都可以正常运行。 我只是想知道,在我没有任何具体路径解析的情况下,它是如何以及为什么起作用的。
我已阅读这个类似问题的答案,但我仍然不明白。
非常感谢帮助!
I have the following output structure of executables in my solution:
%ProgramFiles% | +-[MyAppName] | +-[Client] | | | +-(EXE & several DLL assemblies) | +-[Common] | | | +-[Schema Assemblies] | | | | | +-(several DLL assemblies) | | | +-(several DLL assemblies) | +-[Server] | +-(EXE & several DLL assemblies)
Each project in solution references different DLL assemblies, some of which are outputs from other projects in solution, and others are plain 3rd-party assemblies. For example, [Client] EXE might reference an assembly in [Common], which is in a different directory branch.
All references have "Copy Local" set to false, to mirror the layout of the files in the final installed application.
Now, if I take a look at reference properties in the Visual Studio IDE, I see that "Path" of every reference is absolute and that it corresponds to the actual output location of the assembly. That's understandable and correct. As expected, solution compiles and runs just fine.
What I don't understand is, why everything seems to work even when I close the IDE, rename the [MyAppName] directory and run the [Client] EXE manually? How does the runtime find the assemblies if the reference paths aren't the same as they were at the time of linking?
To be clear - this is actually exactly what I'm after: a semi-dispersed set of application files that run fine regardless of where the [MyAppName] directory is located or even what it's named. I'd just like to know, how and why this works without any specific path resolution on my part.
I've read the answers to this similar question, but I still don't get it.
Help much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
据我所知,唯一的解决方案是在 MyAppBase 中至少有“存根”exe,然后在每个应用程序的 app.config 中将子目录定义为 DLL 的源。
NTFS 确实支持“挂载”其他目录(即硬链接)作为子目录,但它们非常不自动,而且由于除了理论之外我没有尝试过这种方式,所以我无法判断这是否可以作为一种破解。
AFAIK the only solution is to have at least "stub" exes in MyAppBase and then define subdirectories as sources for DLL's in app.config for each application.
NTFS does support "mounting" other directories (ie. hard links) as subdirectories, but they're very unautomatic and since I've not tried going that way except theoretizing, I can't tell if that would work as a hack.
以下是 .Net 查找所需程序集的方式: http: //msdn.microsoft.com/en-us/library/yx7xezcf(VS.71).aspx
第 4 步是您可能感兴趣的内容:http://msdn.microsoft.com/en-us/library/15hyw9x3(VS.71).aspx< /a>
Here's how .Net finds the assemblies that are needed: http://msdn.microsoft.com/en-us/library/yx7xezcf(VS.71).aspx
Step 4 is what might be interesting for you to read: http://msdn.microsoft.com/en-us/library/15hyw9x3(VS.71).aspx
您是否考虑过使用 GACutil 添加您的共享程序集到全局程序集缓存
Have you considered using GACutil to add your shared assemblies to the Global Assembly Cache
也许 Common 文件夹在编译时被指定为 binpath 参数?
(请参阅探测私有 binpath)
http://msdn .microsoft.com/en-us/library/15hyw9x3%28VS.71%29.aspx -
马特
Maybe the Common folder was specified as a binpath parameter when it was compiled?
(See Probing the Private binpath)
http://msdn.microsoft.com/en-us/library/15hyw9x3%28VS.71%29.aspx
-Matt