问题
B"H
I am trying to use the new Tuple features of C# 7, and running into a little bit of an issue. Actually, they were working fine, and I am not sure what changed to make them break.
I am working with an ASP.Net 4
, MVC 5
, targeting .net framework 4.6.1
So in order to use tuples I had to a the Nuget Package 'System.ValueTuple'
. Without it, the project won't compile. It worked fine for a while. Then today when I load any page that uses Tuples I get
Compiler Error Message: CS0433: The type 'ValueTuple' exists in both 'System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' and 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
The project compiles fine, and if I remove System.ValueTuple
then it doesn't compile. But when I run it in a web page I get this error.
My searches on google suggest that ValueTuple
was added to .net 4.7
but I have no reference to 4.7 anywhere in my project and I am not compiling against 4.7. My VS 2017 doesn't even have that option.
Thank you
回答1:
ASP.NET web pages are compiled at runtime, and this compilation ignores the reference assemblies that VS uses. If .NET 4.7 is available at runtime, all of .NET 4.7's types, including its definition of ValueTuple
, will be picked up along with any other definition you might have from other DLLs.
The easiest ways to avoid this problem if you've already installed .NET 4.7 are either to use .NET 4.7 in your development environment as well, or to avoid runtime compilation.
For the former, you'd need to install the .NET 4.7 targeting pack. It should be an option in the Visual Studio installer. When you do that, you can avoid the ValueTuple
NuGet package and thereby the conflict.
For the latter, you can turn on precompilation when you publish your web project. When you do this, all the compilation that normally happens at runtime instead occurs during the publishing, in a controlled way where .NET 4.7's additions won't be seen.
来源:https://stackoverflow.com/questions/46040113/valuetuplet1-t2-exists-in-both-system-valuetuple-and-mscorlib