当它已经被构建系统引用时,添加对 System.Core 的引用 [英] Add a reference to System.Core when it's already referenced by the build system

查看:50
本文介绍了当它已经被构建系统引用时,添加对 System.Core 的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio Intellisense 无法识别 dynamic 关键字,即使项目已生成.我尝试添加对 System.Core 的引用来解决问题.我收到此错误:

Visual Studio Intellisense is not recognizing the dynamic keyword, even though the project builds. I tried to add a reference to System.Core to solve the problem. I receive this error:

无法添加对System.Core"的引用.这个组件是已被构建系统自动引用.

A reference to 'System.Core' could not be added. This component is already automatically referenced by the build system.

我注意到我的其他项目引用了 System.Core.我的解决方法是通过直接编辑 csproj 来添加它,从而修复了 Intellisense.是什么赋予了?为什么 VS 不让我通过 UI 来做?

I noticed that other of my projects reference System.Core. My fix was to add it by editing the csproj directly, which fixed the Intellisense. What gives? Why doesn't VS let me do it through the UI?

推荐答案

在做了一些研究之后,我能找到的只有以前的 SO 票证和一些博客,最终它们都使用了与您相同的解决方法.然后我发现有人在 2011 年在 Microsoft Connect 上提交的错误报告(现在仅在 Wayback Machine,或更易读我的博客),一年左右后,微软的一名员工查克·英格兰最终回答了这个问题.

After doing some research, all I could find were previous SO tickets and some blogs, and ultimately they all ended up using the same work-around you did. Then I found a bug report that someone filed on Microsoft Connect in 2011 (now only available on the Wayback Machine, or in a more readable format on my blog), which was eventually answered a year or so later by Chuck England, a MSFT employee.

TL;DR - 由于 多目标 功能添加到 VS2008(能够更改项目的 .NET Framework 目标).

TL;DR - It was a conscious decision the team made due to the multi-targeting feature added to VS2008 (being able to change a project's .NET Framework target).

由于目标框架需要System.Core",并且删除它会阻止构建(据我了解 Chuck 的回答),他们决定强制引用它,即使您实际上并未在参考文献列表中看到它.

Since the targeted framework requires "System.Core", and removing it would prevent a build (as far as I can understand Chuck's answer), they decided to force a reference to it even if you don't actually see it in the list of references.

有点安全"您可能会说,hack 有一个不良副作用,即当您再次尝试将其添加到引用时会引发该错误,并且您最终需要直接编辑项目文件.

A bit of a "safety" hack, you might say, with an undesirable side-effect that it throws that error when you try to add it to the references again, and you end up needing to edit the project file directly.

Microsoft Connect 已于 2018 年停用,但以下是原始线程的相关部分:

Microsoft Connect was retired in 2018, but here's the relevant parts of the original thread:

是的,体验不是很好.由于 System.Core 是必需的,因此您不应将其删除.即使您删除了引用,我们也会通过为您添加它来解决此问题.[A]您看到的消息表明,System.Core 被隐式引用.因此,您删除它这一事实,除了从项目文件中物理删除一行之外,并没有以任何方式改变构建.

Yes, the experience is not great. Since System.Core is required, you should never remove it. We fixed this by adding it for you even if you remove the reference. [A]s the message you saw indicates, System.Core is implicitly referenced. So, the fact that you have removed it, other than physically removing a line from the project file, has not changed the build in any way.

有些情况下您可能希望能够做到这一点,但这是一个非常前卫的极端情况.但是,我们应该盲目地忽略这样一个事实,即当您将其添加回来时会产生错误.很多这些都是由以前的版本强加给我们的,这些版本不了解多目标定位,而且根本没有得到清理.

There are legitimate scenarios where you might want to be able to do this, but it is a very edgy corner case. However, we should blindly ignore the fact that when you add it back an error is generated. A lot of this was imposed on us by previous versions which did not understand multi-targeting, and simply did not get cleaned up.

[Y]您可以通过右键单击项目节点并选择卸载来手动添加它.再次右键单击项目节点并选择编辑.在编辑器中,复制另一条参考行(例如,System"的那一行)并将其粘贴到同一 ItemGroup 内的原始参考下方.将引用名称更改为System.Core".右键单击项目节点并选择重新加载.选择是"用于保存和重新加载的问题.

[Y]ou can manually add it back by right-clicking on the project node and selecting Unload. Right-click on the project node again and select Edit. In the editor, copy another reference line (for example, the one for "System") and paste it below the original reference inside the same ItemGroup. Change the reference name to "System.Core". Right-click on the project node and select Reload. Choose "yes" for question to save and reload.

VS2008 没有正确处理多目标.它可以让你构建不合法的东西.在 VS2010 中,我们非常努力地确保如果它是为目标框架构建的,那么它将在目标框架上运行.我不能说我知道任何地方不是这样.

VS2008 did not handle multi-targeting correctly. It would allow you to build things that were not legitimate. With VS2010, we have tried very hard to make sure that if it builds for the target framework, then it will run on the target framework. I can't say I know of any places where this is not true.

我们不能回到 VS2008 的方式,因为它不理解多目标.它也不理解真正的配置文件",例如 .NET 4.0 框架的客户端配置文件.事实上,你看到的问题是因为VS2008系统升级为处理多目标,是新的规则集拒绝引用.

We can't go back to the VS2008 way, as it does not understand multi-targeting. Nor did it understand true "profiles", like the Client Profile of the .NET 4.0 framework. In fact, the issue you are seeing is because the VS2008 system was upgraded to handle multi-targeting, and it is the new set of rules that are rejecting the reference.

在这种情况下,即使您删除了引用,我们也会构建.你可能会说,但它不应该建立.但是,它将在目标框架上运行,因此应该已经构建.我们添加了一个必需"的事实对您来说,参考文献在它的开发过程中是一种破碎的体验.

In this case, we build even though you removed a reference. You might say, but it should not have built. But, it will run on the targeted framework, and hence should have built. The fact that we have added a "required" refererence for you is somewhat of a broken experience in how it was developed.

我们真的只是没有足够早地发现这个问题,并且对我们如何修复它以在发布之前获得修复有非常深入的了解.但是,您应该始终引用并因此永远不会删除System.Core"这一事实使这成为无法修复的问题,因为 99% 的客户都不会这样做.

We really just did not catch this early enough, and with a really solid understanding of how we could fix it to get a fix prior to our release. But, the fact that you should always reference, and hence never remove "System.Core", made this a Won't Fix, since this is not something that 99% of customers would ever do.

这篇关于当它已经被构建系统引用时,添加对 System.Core 的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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