繁体   English   中英

C# 7 ValueTuple 编译错误

[英]C# 7 ValueTuple compile error

我正在使用 VS2017 RC,我的应用程序的目标是 net framework 4.6.1。

我有两个引用 System.ValueTuple 4.3 的程序集

MyProject.Services MyProject.WebApi

在 MyProject.Services 我有一个类有这样的方法

public async Task<(int fCount, int cCount, int aCount)> GetAllStatsAsync()
{
    // Some code...
    return (fCount, cCount, aCount);
}

在 MyProject.WebApi 我有一个使用这种方法的控制器:

public async Task<HttpResponseMessage> GetInfoAsync()
{
    // Some code...
    var stats = await _myClass.GetAllStatsAsync();

    var vm = new ViewModel
             {
                 FCount = stats.fCount,
                 CCount = stats.cCount,
                 ACount = stats.aCount
             };

     return Request.CreateResponse(HttpStatusCode.OK, vm);
}

Intellisense 正在工作并解构元组,但是当我编译它时它会失败,并且错误列表窗口中没有任何错误。 在输出窗口中我有这个错误:

2>MyController.cs(83,31,83,40): 错误 CS1061: 'ValueTuple' 不包含'fCount' 的定义,并且找不到接受'ValueTuple' 类型的第一个参数的扩展方法'fCount' (您是否缺少 using 指令或程序集引用?) 2>MyController.cs(84,39,84,49): 错误 CS1061: 'ValueTuple' 不包含 'cCount' 的定义并且没有扩展方法 'cCount' 接受可以找到“ValueTuple”类型的第一个参数(您是否缺少 using 指令或程序集引用?) 2>MyController.cs(85,35,85,40):错误 CS1061:“ValueTuple”不包含定义对于“aCount”并且没有扩展方法“aCount”接受“ValueTuple”类型的第一个参数(您是否缺少 using 指令或程序集引用?)

我尝试添加DEMODEMO_EXPERIMENTAL构建标志,但仍然失败。

知道出了什么问题吗?

编辑1:

此代码有效,并且统计信息被很好地解构。 我可能遇到了一个错误。

public async Task<HttpResponseMessage> GetInfoAsync()
{
    // Some code...
    var stats = await _myClass.GetAllStatsAsync();
    var tu = stats.ToTuple();
    var vm = new ViewModel
             {
                 FCount = tu.Item1,
                 CCount = tu.Item2,
                 ACount = tu.Item3
             };

     return Request.CreateResponse(HttpStatusCode.OK, vm);
}

编辑2:

问题在 github 上打开: https ://github.com/dotnet/roslyn/issues/16200

如果有人陷入同样的​​陷阱,要解决此问题,您需要更新此软件包:Microsoft.Net.Compilers to 2.0(您需要显示预发布)

我只想添加到其他答案。

从引用中删除 System.valuetuple。 否则,它不起作用,我不知道为什么。 基本上,值元组已经在 4.7.2 中,所以如果你使用 Visual Studio 2019,你就准备好了。

如果您使用 Visual Studio 2019 而我使用了,那么这对我有用。 我不知道为什么。 不需要金块。 无需直接引用。

在此处输入图像描述

将您的项目定位为使用 .net 框架 4.7.2 并删除对 valuetuple 的引用。

我认为这是因为您没有定义 fCount、cCount 和 aCount。 尝试这个

public async Task<(int fCount, int cCount, int aCount)> GetAllStatsAsync()
{
    // Some code...
    //fCount, cCount, aCount are not defined here, so you should define them

    var fCount = 0;
    var cCount= 0;
    var aCount = 0;
    return (fCount , cCount, aCount );

    //Other ways:
    //return (fCount : 0, cCount: 0, aCount : 0);
    //return new (int fCount , int cCount, int aCount ) { fCount = 0, cCount = 0, aCount = 0 };
}

public async Task<HttpResponseMessage> GetInfoAsync()
{
    // Some code...
    var stats = await _myClass.GetAllStatsAsync();

    var vm = new ViewModel
             {
                 FCount = stats.fCount,
                 CCount = stats.cCount,
                 ACount = stats.aCount
             };

     return Request.CreateResponse(HttpStatusCode.OK, vm);
}

使用@Swell 建议编辑

看看这个帖子

我的问题不是在编译错误,而是在运行时。

但我怀疑修复仍然有效,也可以帮助这里的人。

将我的项目切换到 .Net 框架 4.7.2 后,我不得不手动将 System.ValueTuple 的提示路径从 net461 更新为 net47

<HintPath>..\Solutions\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM