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:
A reference to 'System.Core' could not be added. This component is already automatically referenced by the build system.
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?
After Googling around a little bit I found that the common answer to this question was to add a reference to System. Web. dll by finding it in the list presented by right-clicking on References in the Solution Explorer and clicking "Add Reference...".
One method of adding references to your library is by typing it directly in the project. json file. As you can see that we have added some references under the dependencies section as shown in the following code. Let us now save this file and you will see that references are added to your library now.
You can also right-click the project node and select Add > Project Reference. If you see a References node in Solution Explorer, you can use the right-click context menu to choose Add Reference. Or, right-click the project node and select Add > Reference.
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 - 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).
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.
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 was retired in 2018, but here's the relevant parts of the original thread:
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]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 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With