Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

c# 7.0 - C# 7 ValueTuple compile error

I'm using VS2017 RC and my application targets net framework 4.6.1.

I have two assemblies referencing System.ValueTuple 4.3

MyProject.Services MyProject.WebApi

In MyProject.Services I have a class with a method like this

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

In MyProject.WebApi I have a controller that use this method like that:

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 is working and deconstruct the tuple but when I compile it fails without any Error in Error List window. In the output windows I have this errors:

2>MyController.cs(83,31,83,40): error CS1061: 'ValueTuple' does not contain a definition for 'fCount' and no extension method 'fCount' accepting a first argument of type 'ValueTuple' could be found (are you missing a using directive or an assembly reference?) 2>MyController.cs(84,39,84,49): error CS1061: 'ValueTuple' does not contain a definition for 'cCount' and no extension method 'cCount' accepting a first argument of type 'ValueTuple' could be found (are you missing a using directive or an assembly reference?) 2>MyController.cs(85,35,85,40): error CS1061: 'ValueTuple' does not contain a definition for 'aCount' and no extension method 'aCount' accepting a first argument of type 'ValueTuple' could be found (are you missing a using directive or an assembly reference?)

I tried adding the DEMO and DEMO_EXPERIMENTAL build flags but still fails.

Any idea on what's wrong?

EDIT 1:

This code works and stats is well deconstructed. I'm probably hitting a bug.

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);
}

EDIT 2:

Issue open on github here: https://github.com/dotnet/roslyn/issues/16200

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If anyone falls in the same trap, to fix this you need to update this package: Microsoft.Net.Compilers to 2.0 (you need to show pre-release)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...