元组Lambda语法突然在C#中不起作用 [英] Tuple lambda syntax suddenly not working in C#

查看:60
本文介绍了元组Lambda语法突然在C#中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.NET Framework 4.7,我已经实现了几种这样的语法

I'm using .NET Framework 4.7, I have implemented several of syntax like this

Task<(bool isSuccess, string message)> DownloadFileResource()

它已经可以通过var

var downloadResult = await BlobUtils.DownloadFileResource()

但是今天,当我打开Visual Studio时,我的所有var转换为dynamic类型都是IDE的建议,不再是(bool isSuccess, string message),并且在运行它时会抛出错误

But today when I open visual studio, all my var transform to dynamic type as a suggestion from IDE, not a (bool isSuccess, string message) anymore, and throw error when running it

 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:
 'System.ValueTuple<bool,string>' does not contain a definition for > 'isSuccess'
    at CallSite.Target(Closure , CallSite , Object )
    at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite  site, T0 arg0)
    at DataUnitCrawler.MainFormV3.<DownloadFileResource>d__10.MoveNext() in D:\Gannha\GannhaDesktopApp\DataUnitCrawler\MainFormV3.cs:line 97
 --- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
 at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
 at DataUnitCrawler.MainFormV3.<<-ctor>b__9_1>d.MoveNext() in D:\Gannha\GannhaDesktopApp\DataUnitCrawler\MainFormV3.cs:line 86
 --- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_0(Object state)


************** Loaded Assemblies ************** mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4121.0 built by: NET48REL1LAST_C
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
---------------------------------------- DataUnitCrawler
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///D:/Gannha/GannhaDesktopApp/DataUnitCrawler/bin/Debug/DataUnitCrawler.exe
---------------------------------------- System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4121.0 built by: NET48REL1LAST_C
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
---------------------------------------- System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4001.0 built by: NET48REL1LAST_C
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
---------------------------------------- System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.3752.0 built by: NET48REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
---------------------------------------- System.Data
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4121.0 built by: NET48REL1LAST_C
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll
---------------------------------------- System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4121.0 built by: NET48REL1LAST_C
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
---------------------------------------- System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.3752.0 built by: NET48REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
---------------------------------------- DataUtility
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///D:/Gannha/GannhaDesktopApp/DataUnitCrawler/bin/Debug/DataUtility.DLL
---------------------------------------- Microsoft.WindowsAzure.Storage
    Assembly Version: 9.3.2.0
    Win32 Version: 9.3.2.0
    CodeBase: file:///D:/Gannha/GannhaDesktopApp/DataUnitCrawler/bin/Debug/Microsoft.WindowsAzure.Storage.DLL
---------------------------------------- Microsoft.CSharp
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.3752.0
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.CSharp/v4.0_4.0.0.0__b03f5f7f11d50a3a/Microsoft.CSharp.dll
---------------------------------------- System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.3752.0 built by: NET48REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
---------------------------------------- Newtonsoft.Json
    Assembly Version: 12.0.0.0
    Win32 Version: 12.0.1.22727
    CodeBase: file:///D:/Gannha/GannhaDesktopApp/DataUnitCrawler/bin/Debug/Newtonsoft.Json.DLL
---------------------------------------- System.Numerics
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.3752.0 built by: NET48REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll
---------------------------------------- System.Runtime.Serialization
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4121.0 built by: NET48REL1LAST_C
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Serialization/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.Serialization.dll
---------------------------------------- System.Dynamic
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.3752.0
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Dynamic/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Dynamic.dll
---------------------------------------- Anonymously Hosted DynamicMethods Assembly
    Assembly Version: 0.0.0.0
    Win32 Version: 4.8.4121.0 built by: NET48REL1LAST_C
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll
---------------------------------------- NLog
    Assembly Version: 4.0.0.0
    Win32 Version: 4.5.11.8645
    CodeBase: file:///D:/Gannha/GannhaDesktopApp/DataUnitCrawler/bin/Debug/NLog.DLL
---------------------------------------- NLog.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.2.3.167
    CodeBase: file:///D:/Gannha/GannhaDesktopApp/DataUnitCrawler/bin/Debug/NLog.Windows.Forms.DLL
---------------------------------------- System.ServiceModel
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4121.0 built by: NET48REL1LAST_C
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.ServiceModel/v4.0_4.0.0.0__b77a5c561934e089/System.ServiceModel.dll
---------------------------------------- Microsoft.Azure.Storage.Blob
    Assembly Version: 11.1.1.0
    Win32 Version: 11.1.1
    CodeBase: file:///D:/Gannha/GannhaDesktopApp/DataUnitCrawler/bin/Debug/Microsoft.Azure.Storage.Blob.DLL
---------------------------------------- Microsoft.Azure.Storage.Common
    Assembly Version: 11.1.1.0
    Win32 Version: 11.1.1.0
    CodeBase: file:///D:/Gannha/GannhaDesktopApp/DataUnitCrawler/bin/Debug/Microsoft.Azure.Storage.Common.DLL
---------------------------------------- System.Net.Http
    Assembly Version: 4.2.0.0
    Win32 Version: 4.6.26011.01
    CodeBase: file:///D:/Gannha/GannhaDesktopApp/DataUnitCrawler/bin/Debug/System.Net.Http.DLL
----------------------------------------

************** JIT Debugging ************** To enable just-in-time (JIT) debugging, the .config file for this application or computer
(machine.config) must have the jitDebugging value set in the
system.windows.forms section. The application must also be compiled
with debugging enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" /> </configuration>

When JIT debugging is enabled, any unhandled exception will be sent to
the JIT debugger registered on the computer rather than be handled by
this dialog box.

当我放置一个断点时,它们显示item1item2而不是isSuccessmessage

When I put a breakpoint, they show item1, item2 instead of isSuccess, message

我通过明确声明进行了测试,它是否有效?真是很困惑

I tested by declaring explicitly and it worked? That's very confused

简而言之,问题是我已经使用(bool isSuccess, string message)函数类型语法的ValueTuple很长时间了,并且在var中可以正常获取值. var知道期望的类型,直到今天它带有dynamic类型,并且没有所有isSuccessmessage属性.

For short, the problem is I have used ValueTuple of (bool isSuccess, string message) function type syntax for a long time and got value normally with var. The var knew expected the type until today it comes with dynamic type and off all isSuccess, message attributes.

Shorter:我的期望:(我在.Net Core 2.1中进行了测试)

Shorter: what I expected: (I tested in .Net Core 2.1)

IDE的作用:

如何解决?是个错误吗?

How to solve it? It's a bug?

推荐答案

isSuccess不是ValueTuple<bool, string>的字段-这只是编译器附加到ValueTuple<bool, string>.Item的名称,因此,dynamic不能到达它.

isSuccess is not a field of ValueTuple<bool, string>- It's just a name the compiler attahces to ValueTuple<bool, string>.Item and, thus, dynamic can't get to it.

我非常怀疑IDE的建议dynamic该建议的IDE代码是什么?

I very much doubt the IDE suggested dynamic what was the IDE code of that suggestion?

更新

我正在使用Microsoft Visual Studio Enterprise 2019版本16.4.6,它非常清楚result2dynamic并且result2.messagedynamic.

I'm using Microsoft Visual Studio Enterprise 2019 Version 16.4.6 and it knows pretty well that result2 is dynamic and that result2.message is dynamic.

dynamic表示它将在运行时解析,其中result2将是ValueTuple<bool, string>,没有message属性或字段.

dynamic means that it will be resolved at run-time where result2 will be a ValueTuple<bool, string>, which doesn't have a message property or field.

这是一个错误,但是您的代码中有一个错误,请注意IDE.

It's a bug, but a bug in your code, note the IDE.

您可以看看你的代码的

You can look at your code in sharplab.io. On the right side you'll see the translated code and hovering the source code (on the left), you'll see tooltips explaining the code.

这篇关于元组Lambda语法突然在C#中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆