visual-studio Visual Studio 引用和版本控制 - 它是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/186643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Studio References and versioning - how does it work?
提问by alexmac
We have several common libs. Ideally we want them all to use the latest version of a dll even if they have been compiled against an older different version (assume the latest version is backward compatible)
我们有几个常用的库。理想情况下,我们希望它们都使用最新版本的 dll,即使它们是针对较旧的不同版本编译的(假设最新版本向后兼容)
e.g we have:
Project dll
Common controls dll
Logging dll
Database access dll
例如,我们有:
项目 dll
公共控件 dll
日志 dll
数据库访问 dll
Project and common controls reference v2 of database dll. Logging however references v1.
项目和通用控件参考数据库 dll 的 v2。然而,日志记录引用了 v1。
If different versions are referenced in different components how does VS pick which one to use?
Do we have to recompile v1 dll to use the latest database (v2) or can we get this to pick up automatically?
Is it possible to force a particular version to be used?
如果在不同的组件中引用了不同的版本,VS 如何选择使用哪一个?
我们是否必须重新编译 v1 dll 才能使用最新的数据库 (v2) 或者我们可以自动获取它吗?
是否可以强制使用特定版本?
Thanks,
谢谢,
Alex
亚历克斯
采纳答案by Jon Skeet
By default, for versioned DLLs I believe VS will force an exact match. If you look in the properties of the reference, however, you'll find a property called "Specific Version". Set that to "false" and it will match later versions.
默认情况下,对于版本化的 DLL,我相信 VS 会强制进行精确匹配。但是,如果您查看引用的属性,您会发现一个名为“特定版本”的属性。将其设置为“false”,它将匹配更高版本。
I don't have the full version of VS with me to find an appropriate MSDN link, unfortunately.
不幸的是,我没有完整版的 VS 来查找合适的 MSDN 链接。
In addition, you can use the assemblyBindingelement in app.config.
此外,您可以使用app.config 中的assemblyBinding元素。
回答by Jon Skeet
There are actually two scenarios here - using strong names/GAC, or not using strong names.
这里实际上有两种情况 - 使用强名称/GAC,或不使用强名称。
If you're using strong names, and installing the components into the GAC, then .Net will want to use the version of the component that the client was referencing when it was compiled. So in your example it would be quite possible for Project to reference database V2, while Logging referenced database V1, because the DLLs can be stored in parallel in the GAC. So if you actually want Logging to use V2 instead of V1, you will need to modify configuration files to say that "a reference to V1 should be pointed on to V2". There are different places to do this - app file, machine file etc.
如果您使用强名称并将组件安装到 GAC 中,那么 .Net 将希望使用客户端在编译时引用的组件版本。因此,在您的示例中,Project 很可能引用数据库 V2,而 Logging 引用数据库 V1,因为 DLL 可以并行存储在 GAC 中。因此,如果您确实希望 Logging 使用 V2 而不是 V1,则需要修改配置文件以说明“对 V1 的引用应指向 V2”。有不同的地方可以做到这一点 - 应用程序文件、机器文件等。
If you're not using strong names, then .Net will by default use the version of the DLL that's in the same folder as the client. So suppose you deploy Project, CommonControls and Logging in the same folder as database V2. Then even if Logging was built against database V1, it will attempt to use the component in the same folder, ie database V2. As long as V2 can supply the same public classes and methods that Logging wants to use, it will work fine.
如果您不使用强名称,则 .Net 将默认使用与客户端位于同一文件夹中的 DLL 版本。因此,假设您将 Project、CommonControls 和 Logging 部署在与数据库 V2 相同的文件夹中。那么即使 Logging 是针对数据库 V1 构建的,它也会尝试使用同一文件夹中的组件,即数据库 V2。只要 V2 可以提供 Logging 想要使用的相同公共类和方法,它就可以正常工作。
In my environment - where all our applications are internal - we don't use the GAC. We just deploy all the files that the application needs into a single folder. When you have a lot of common components, it would just be a nightmare to keep the configuration files in synch.
在我的环境中 - 我们所有的应用程序都在内部 - 我们不使用 GAC。我们只是将应用程序需要的所有文件部署到一个文件夹中。当您有很多通用组件时,保持配置文件同步简直就是一场噩梦。
This is all very different to COM, where all applications picked up the currently registered copy of the DLL (assuming V1 and V2 were binary compatible).
这与 COM 完全不同,在 COM 中所有应用程序都获取当前注册的 DLL 副本(假设 V1 和 V2 是二进制兼容的)。
回答by alexmac
I came across this site which has a couple of suggestions: http://blog.fredrikhaglund.se/blog/2008/02/23/get-control-over-your-assembly-dependencies/
我发现这个网站有几个建议:http: //blog.fredrikhaglund.se/blog/2008/02/23/get-control-over-your-assembly-dependencies/
To get a specific version apparently you have to edit web.config or app.config?
要获得特定版本显然您必须编辑 web.config 或 app.config?
"Finally, take a look at your csproj file using notepad (or unload the project in Visual Studio to be able to open it as text). When you add references using Visual Studio you will get a assembly reference with both version number and public key and this can give you some trouble when you upgrade a vendor assembly to a newer version."
http://blog.fredrikhaglund.se/blog/2008/02/23/get-control-over-your-assembly-dependencies/
“最后,使用记事本查看您的 csproj 文件(或在 Visual Studio 中卸载项目以将其作为文本打开)。当您使用 Visual Studio 添加引用时,您将获得带有版本号和公钥的程序集引用当您将供应商程序集升级到更新版本时,这可能会给您带来一些麻烦。”
http://blog.fredrikhaglund.se/blog/2008/02/23/get-control-over-your-assembly-dependencies/
回答by Matt Brown
If you have the source, you could use project references instead of dll references. That way you would always get the latest.
如果您有源代码,则可以使用项目引用而不是 dll 引用。这样你总是会得到最新的。

