强制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

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

问题描述

我已经在Visual Studio 2015中编写了一个使用C#6.0功能并针对.NET 4.5.2的应用程序.当我使用TeamCity服务器完成的Microsoft Build Tools 2015进行构建时,生成的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")

被解释为(链接-仅存在于.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天全站免登陆