I've installed Visual Studio 15 Preview 3 and tried to use the new tuple feature
static void Main(string[] args)
{
var x = DoSomething();
Console.WriteLine(x.x);
}
static (int x, int y) DoSomething()
{
return (1, 2);
}
When I compile I get the error:
Predefined type 'System.ValueTuple´2´ is not defined or imported
According to the blog post, this features should be "on" by default.
What did I do wrong?
For .NET 4.6.2 or lower, .NET Core 1.x, and .NET Standard 1.x you need to install the NuGet package System.ValueTuple
:
Install-Package "System.ValueTuple"
Or using a package reference in VS 2017:
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
.NET Framework 4.7, .NET Core 2.0, and .NET Standard 2.0 include these types.
It's part of the .NET Framework 4.7
.
As long as you don't target the above framework or higher (or .NET Core 2.0
/ .NET Standard 2.0
), you'll need to reference ValueTuple
. Do this by adding the System.ValueTuple
NuGet Package
The ValueTuple types are built into newer frameworks:
- .NET Framework 4.7
- .NET Core 2.0
- Mono 5.0
- .Net Standard 2.0
Until you target one of those newer framework versions, you need to reference the ValueTuple package.
More details at http://blog.monstuff.com/archives/2017/03/valuetuple-availability.html
For Visual Studio Code use the built in Terminal and run:
dotnet add package "System.ValueTuple"
Don't forget to run dotnet restore
afterwards.
Make sure you have .NET 4.6.2 Developer Pack for VS installed and then pull in System.ValueTuple
package from NuGet.
In case others have the same problem, I ran into this error after updating a project to 4.7. Oddly enough, I had to remove the System.ValueTuple reference for this error to go away.
I would not advise adding ValueTuple
as a package reference to the .net Framework projects. As you know this assembly is available from 4.7 .NET Framework.
There can be certain situations when your project will try to include at all costs ValueTuple
from .NET Framework folder instead of package folder and it can cause some assembly not found errors.
We had this problem today in company. We had solution with 2 projects (I oversimplify that) :
Lib
Web
Lib
was including ValueTuple and Web
was using Lib
. It turned out that by some unknown reason Web
when trying to resolve path to ValueTuple
was having HintPath
into .NET Framework directory and was taking incorrect version. Our application was crashing because of that. ValueTuple
was not defined in .csproj
of Web
nor HintPath
for that assembly. The problem was very weird. Normally it would copy the assembly from package folder. This time was not normal.
For me it is always risk to add System.*
package references. They are often like time-bomb. They are fine at start and they can explode in your face in the worst moment. My rule of thumb: Do not use System.*
Nuget package for .NET Framework if there is no real need for them.
We resolved our problem by adding manually ValueTuple
into .csproj
file inside Web
project.
I had to check System.ValueTuple.dll file was under source control and correct its reference in .cssproj files:
- rightclick each project in solution
- unload project
- edit .cssproj file: change
< Reference Include="System.ValueTuple" >
< HintPath >
....\ProjectName\ProjectName\obj\Release\Package\PackageTmp\bin\System.ValueTuple.dll
< /HintPath >
< /Reference >
into
< Reference Include="System.ValueTuple" >
< HintPath >
..\packages\System.ValueTuple.4.4.0\lib\netstandard1.0\System.ValueTuple.dll
< /HintPath >
< /Reference >
- save changes and reload projects
- find System.ValueTuple.dll and save it into this folder
- add reference of this file into source control
(Optional): 7. solve same problems with another .dll files this way
We were seeing this same issue in one of our old projects that was targeting Framework 4.5.2. I tried several scenarios including all of the ones listed above: target 4.6.1, add System.ValueTuple package, delete bin, obj, and .vs folders. No dice. Repeat the same process for 4.7.2. Then tried removing the System.ValueTuple package since I was targeting 4.7.2 as one commenter suggested. Still nothing. Checked csproj file reference path. Looks right. Even dropped back down to 4.5.2 and installing the package again. All this with several VS restarts and deleting the same folders several times. Literally nothing worked.
I had to refactor to use a struct instead. I hope others don't continue to run into this issue in the future but thought this might be helpful if you end up as stumped up as we were.
来源:https://stackoverflow.com/questions/38382971/predefined-type-system-valuetuple%c2%b42%c2%b4-is-not-defined-or-imported