我已经安装了Visual Studio 15 Preview 3,并尝试使用新的元组功能
static void Main(string[] args)
{
var x = DoSomething();
Console.WriteLine(x.x);
}
static (int x, int y) DoSomething()
{
return (1, 2);
}
当我编译时,我得到错误:
预定义类型“系统”。ValueTuple´2´未定义或导入
根据这篇博文,这个功能在默认情况下应该是“开启”的。
我做错了什么?
当我从。net 4.6.2升级到。net 4.7.2时,我也遇到了这个问题。不幸的是,我无法删除对系统的包引用。ValueTuple,因为我使用的另一个NuGet包依赖于它。
最终我找到了根本原因:在项目文件夹中有一个。net 4.6.2版本的mscorlib.dll(发布操作的输出),MSBuild决定引用这个程序集,而不是位于C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2的官方。net 4.7.2参考程序集。
由于系统。ValueTuple是在。net 4.7中引入的,MSBuild编译失败,因为它在。net 4.6.2的参考程序集中找不到该类型。
(https://stackoverflow.com/a/57777123/128709副本)
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.
我不得不重构以使用结构体代替。我希望其他人将来不要继续遇到这个问题,但如果你最终像我们一样被难住了,这可能会有帮助。