c# - 为什么在混合 C# ValueTuple 和动态时出现此编译器错误

标签 c# dynamic roslyn c#-7.0

当使用 ValueTuple 和动态对象时,我收到了这个奇怪的 CS8133 错误。我将动态对象作为输入传递,并将 ValueTuple 作为输出。为什么会互相影响。

public static (string, string) foo(dynamic input)
{
    return ("", "");
}

public void foo_test()
{
    dynamic input = new { a = "", b = "" };
    (string v1, string v2) = foo(new { a = "", b = "" }); //compiles fine
    (string v3, string v4) = foo(input); //CS8133 Cannot deconstruct dynamic objects
    var result = foo(input);  //compiles fine
}

编辑: 错误信息是:CS8133 Cannot deconstruct dynamic objects

最佳答案

参见 the feature spec :

The resolution is equivalent to typing rhs.Deconstruct(out var x1, out var x2, ...); with the appropriate number of parameters to deconstruct into. It is based on normal overload resolution. This implies that rhs cannot be dynamic and that none of the parameters of the Deconstruct method can be type arguments. ...

重要的部分是var。 在正常的重载解析中,我们可以从发现的 Deconstruct 方法中推断出类型。但是对于动态方法调用,您无法获得编译时类型信息,因此 var 类型必然未被推断(即,这是一个错误)。

更一般地说,这就是为什么不能在动态调用中使用 out var(out var 本地的 var 类型是什么?)。

关于c# - 为什么在混合 C# ValueTuple 和动态时出现此编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45384540/

相关文章:

c# - 如何从 foreach gameobject.transform 获取最后几个元素

javascript - 使用 JQuery mobile 创建 ListView 无法正常工作 II

java - Java 中的动态时间序列堆积面积图

c# - 如何找到 C# 项目中的所有硬编码值(解决方案)?

c# - 过度使用异步等待?

c# - MvcSiteMapProvider——动态节点的子节点不会出现在面包屑或站点地图中

c# - 事件监听器委托(delegate)中的自引用 (C#)

c# - 带有 EnableSSL 的 FtpWebRequest

c# - Roslyn CodeFix 和重构之间有什么区别?

text - ImageMagick最适合矩形内的文本?