强制 Microsoft Build Tools 2015 为目标版本的框架而不是 4.6 包含 mscorlib [英] Force Microsoft Build Tools 2015 to include mscorlib for the targeted version of the framework instead of 4.6

查看:27
本文介绍了强制 Microsoft Build Tools 2015 为目标版本的框架而不是 4.6 包含 mscorlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Visual Studio 2015 中编写了一个应用程序,该应用程序使用 C# 6.0 功能并面向 .NET 4.5.2.当我使用 Microsoft Build Tools 2015 构建它时,这是由我们的 TeamCity 服务器完成的,生成的 bin 文件夹还包含 mscorlib.dll 的副本.这里的问题是被复制的 mscorlib.dll 是 .NET 4.6 DLL,这会导致运行时出现问题.

我已经用新的字符串插值语法替换了对 string.Format() 的调用来解决这个问题.然而,这将根本问题推到了底层,而不是解决它:为什么 .NET 4.6 DLL 包含在我的构建中,我如何强制将 4.5.2 DLL 包含在它的位置?

如果您对这对我造成的运行时问题感兴趣,它会导致我的:

string.Format(CultureInfo.InvariantCulture, "{0}='{1}'", "key", "value")

被解释为 (link -- 仅存在于 .NET 4.6 中):

System.String System.String.Format(System.IFormatProvider, System.String, System.Object, System.Object)

而不是(链接):

System.String System.String.Format(System.IFormatProvider, System.String, params System.Object[])

解决方案

在 TeamCity 生成代理上生成解决方案时,TeamCity 使用您已安装在该代理上的 MSBuild 版本.创建每个版本的 MSBuild 以构建针对特定版本 .NET 的 DLL.例如,我认为 MSBuild 2015 的目标是 .NET 4.6.

为了使用 MSBuild 2015 构建面向 .NET 4.5.2 的 DLL,您还需要安装所谓的 .NET 4.5.2 目标包(在某些地方也称为开发包):

如果您没有安装目标包并尝试使用错误的构建工具针对旧版本的 .NET 进行构建,就像我的情况一样,那么 MSBuild 2015 会尝试通过构建它知道的唯一方式来进行补偿:通过遵循.NET 4.6 的规则(无论如何都应该向后兼容 4.5.2)并在构建文件夹中放入 .NET DLL (mscorlib.dll).

在大多数情况下,这仍然可以正常工作,除了我的代码使用了 String.Format(),它在 4.5.2 和 4.6 之间发生了非常微妙的重大变化.

I have written an application in Visual Studio 2015 that uses C# 6.0 features and targets .NET 4.5.2. When I build it using Microsoft Build Tools 2015, which is what is done by our TeamCity server, the resulting bin folder also contains a copy of mscorlib.dll. The problem here is that the mscorlib.dll being copied is the .NET 4.6 DLL, which causes problems at runtime.

I have replaced my call to string.Format() with the new string interpolation syntax to work around the problem. That, however, shoves the underlying problem under the carpet instead of addressing it: Why is the .NET 4.6 DLL included in my build and how can I force the 4.5.2 DLL to be included in its place?

If you are interested in the runtime problem this caused for me, it caused my:

string.Format(CultureInfo.InvariantCulture, "{0}='{1}'", "key", "value")

To be interpreted as (link -- which only exists in .NET 4.6):

System.String System.String.Format(System.IFormatProvider, System.String, System.Object, System.Object)

Instead of (link):

System.String System.String.Format(System.IFormatProvider, System.String, params System.Object[])

解决方案

When you build your solution on a TeamCity build agent, TeamCity uses a version of MSBuild that you had installed on the agent. Each version of MSBuild is created to build DLLs that target a specific version of .NET. For example, I think that MSBuild 2015 targets .NET 4.6.

In order to use MSBuild 2015 to build DLLs that target .NET 4.5.2, you need to also install what is called a .NET 4.5.2 targeting pack (also referred to as development pack in some places):

If you do not install the targeting pack and try to build against an older version of .NET with the wrong build tools, as was my case, then MSBuild 2015 tries to compensate by building the only way it knows how: By following the rules of .NET 4.6 (which should be backwards-compatible to 4.5.2 anyway) and throwing in a .NET DLL (mscorlib.dll) in the build folder.

In most cases, this would have still worked fine, except my code made use of String.Format() which suffered a very subtle breaking change between 4.5.2 and 4.6.

这篇关于强制 Microsoft Build Tools 2015 为目标版本的框架而不是 4.6 包含 mscorlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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