为什么带有引用程序集的VS2012项目无法自动定位到4.0 [英] Why VS2012 project with referenced assembly can't target 4.0 automatically

查看:162
本文介绍了为什么带有引用程序集的VS2012项目无法自动定位到4.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio 2012 C#控制台应用程序中,我将".NET Framework Target"从4.5降级到4.0.装有两个Framework的Win 7 Pro.

In a Visual Studio 2012 C# console application, I downgrade ".NET Framework Target" from 4.5 to 4.0. Win 7 Pro with both Frameworks installed.

然后我引用一个程序集,该程序集通过警告抱怨以下内容:

I then reference an assembly, which, through warnings complains the following:

The primary reference "System.Threading.Tasks.Dataflow, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" could not be resolved because it has an indirect dependency on the framework assembly "System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "System.Threading.Tasks.Dataflow, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" or retarget your application to a framework version which contains "System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".

如果我此时尝试进行编译,则会出错,因为引用的程序集的类型和名称空间不可用,就好像根本没有引用该程序集一样.

If I try to compile at this point, I error, becuase types and namespaces from referenced assembly aren't available, as if the assembly was not referenced at all.

添加引用"对话框没有任何 System.Runtime选项,但是如果我手动浏览到 C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ 并引用在那里找到的System.Runtime程序集,警告消失了,并且我能够进行编译.

The "Add Reference" dialog doesn't have any System.Runtime choice, but if I manually brose to C:\Windows\Microsoft.NET\Framework\v4.0.30319\ and reference the System.Runtime assembly found there, warnings go away and I am able to compile.

问题:

  1. 是对System.Runtime版本的强制执行吗?这是潜在的问题(部署).

  1. Is such forcing of the System.Runtime version a potential issue down the road (deployment).

如果将VS Project属性设置为目标Framework 4.0(与以4.0 SystemRuntime/CLR为目标无关),为什么引用的DLL不能选择该属性,以及为什么手动将引用添加到我的项目中解决该问题?

If VS Project properties are seto to target Framework 4.0 (doesn't that relate to targeting the 4.0 SystemRuntime/CLR), why isn't the refferenced DLL picking that up and why manually adding the reference to my project fixes that issue?

推荐答案

即使库System.Runtime位于C:\Windows\Microsoft.NET\Framework\v4.0.30319\目录中,它也不是.NET 4.0框架的一部分. .NET 4.5是4.0的就地更新,并安装在具有相同版本号的同一文件夹中.

Even though the library System.Runtime is inside the C:\Windows\Microsoft.NET\Framework\v4.0.30319\ directory, it is not part of the .NET 4.0 framework. .NET 4.5 is an in-place update of 4.0 and is installed in the same folder with the same version number.

下面是一个截图,证明该库在播放.NET 4.0安装中不存在:

Here is a screenshot that proofs that the library does not exist on a play .NET 4.0 installation:

您也可以通过浏览到C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework目录来验证这一点,在该目录中可以找到所有已安装框架版本的原始程序集.您会在.NETCore\v4.5.NETPortable\v4.5子目录的一部分中找到System.Runtime.dll.

You can also validate this by browsing to the C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework directory where you find the original assemblies for all installed framework versions. You will find the System.Runtime.dll as part of the .NETCore\v4.5 and .NETPortable\v4.5 subdirectories.

之所以可以将库添加到项目中,是因为运行时在4.0和4.5之间没有变化,因此Visual Studio不知道甚至不关心手动添加的库是否已由4.5安装.在这种情况下,Visual Studio中的目标只是一个过滤器,可以避免意外地将4.5程序集添加到目标为4.0的项目中.

The reason that you can add the library to your project is that the runtime did not change between 4.0 and 4.5, so Visual Studio doesn't know or even care that the library you added manually is installed by 4.5. In this case the targetting in Visual Studio is only a filter that avoids that you accidentally add a 4.5 assembly to a project that targets 4.0.

其他信息:

Rick Strahl在该主题上有一篇非常不错的博客文章,其中进行了更详细的分析:

Rick Strahl has a very good blog post on the topic with a more detailed analysis:

http://www.west-wind.com/weblog/posts/2012/Mar/13/NET-45-is-an-inplace-replacement-for-NET-40

这篇关于为什么带有引用程序集的VS2012项目无法自动定位到4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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