Predefined type 'System.ValueTuple´2´ is not defined or imported

≡放荡痞女 提交于 2019-11-26 14:21:40

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.

Nikita

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:

  1. rightclick each project in solution
  2. unload project
  3. 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 >

  1. save changes and reload projects
  2. find System.ValueTuple.dll and save it into this folder
  3. 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!