【问题标题】:VS2019 IDE and command line compiler disagree on C# statementsVS2019 IDE 和命令行编译器在 C# 语句上存在分歧
【发布时间】:2019-08-22 02:33:22
【问题描述】:

所以在 IDE 中我有以下内容:

    public static void ConnectToOperaObjects(ref Microsoft.ClearScript.Windows.JScriptEngine jSE)
    {
        foreach (Tuple<string, object> tso in new List<Tuple<string, object>>() {
            (name: "CSOperaDriver", type: typeof(OpenQA.Selenium.Opera.OperaDriver)),
            (name: "CSOperaDriverService", type: typeof(OpenQA.Selenium.Opera.OperaDriverService)),
            (name: "CSOperaOptions", type: typeof(OpenQA.Selenium.Opera.OperaOptions)) })
        {
            jSE.AddHostType(tso.name, tso.type);
        }
    }

IDE 毫无疑问地编译它。命令行编译器抱怨

Objects.cs(161,17): error CS1950: The best overloaded Add method 'List<Tuple<string, object>>.Add(Tuple<string, object>)' for the collection initializer has some invalid arguments [C:\Users\bugma\Source\Repos\RR\RR\RR.csproj]
Objects.cs(161,17): error CS1503: Argument 1: cannot convert from '(string, System.Type)' to 'Tuple<string, object>' [C:\Users\bugma\Source\Repos\RR\RR\RR.csproj]
Objects.cs(162,17): error CS1950: The best overloaded Add method 'List<Tuple<string, object>>.Add(Tuple<string, object>)' for the collection initializer has some invalid arguments [C:\Users\bugma\Source\Repos\RR\RR\RR.csproj]
Objects.cs(162,17): error CS1503: Argument 1: cannot convert from '(string, System.Type)' to 'Tuple<string, object>' [C:\Users\bugma\Source\Repos\RR\RR\RR.csproj]
Objects.cs(163,17): error CS1950: The best overloaded Add method 'List<Tuple<string, object>>.Add(Tuple<string, object>)' for the collection initializer has some invalid arguments [C:\Users\bugma\Source\Repos\RR\RR\RR.csproj]
Objects.cs(163,17): error CS1503: Argument 1: cannot convert from '(string, System.Type)' to 'Tuple<string, object>' [C:\Users\bugma\Source\Repos\RR\RR\RR.csproj]
Objects.cs(165,42): error CS1061: 'Tuple<string, object>' does not contain a definition for 'name' and no accessible extension method 'name' accepting a first argument of type 'Tuple<string, object>' could be found (are you missing a using directive or an assembly reference?) [C:\Users\bugma\Source\Repos\RR\RR\RR.csproj]
Objects.cs(165,52): error CS1061: 'Tuple<string, object>' does not contain a definition for 'type' and no accessible extension method 'type' accepting a first argument of type 'Tuple<string, object>' could be found (are you missing a using directive or an assembly reference?) [C:\Users\bugma\Source\Repos\RR\RR\RR.csproj]

命令行工具被调用为

"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\amd64\MSBuild.exe" %* /t:Build

使用 RR 的 csproj 文件作为参数。我已经尝试为它提供sln 文件,但没有效果。

解决方法是什么?

【问题讨论】:

  • 建议不要编辑帖子,而是创建一个自我回答,似乎更有用

标签: c# visual-studio msbuild


【解决方案1】:

我会将您的Tuple 更改为以下命名 ValueTuple C# 7 语法,它应该可以工作:

var list = new List<(string name, object type)>()
          {
             (name: "CSOperaDriver", type: typeof(OpenQA.Selenium.Opera.OperaDriver))
             ...
          };

foreach (var tso in list)
   ...

或者像这样改变你的初始化器

new Tuple<string, object>( "CSOperaDriver",  typeof(OpenQA.Selenium.Opera.OperaDriver))),

我的 spidey 感觉 告诉我 Tuple 使用 Named ValueTuple 语法导致您出现问题。

【讨论】:

  • 我刚刚成功地将 foreach 子句更改为 var tso in new[] { ...
【解决方案2】:

感谢@TheGeneral 建议更改为ValueTuple。有趣的是,该类型不喜欢成为对象,而是坚持成为System.Type

    public static void ConnectToOperaObjects(ref Microsoft.ClearScript.Windows.JScriptEngine jSE)
    {
        foreach (var tso in new List<(string name, System.Type type)>() {
            ("CSOperaDriver", typeof(OpenQA.Selenium.Opera.OperaDriver)),
            ("CSOperaDriverService", typeof(OpenQA.Selenium.Opera.OperaDriverService)),
            ("CSOperaOptions", typeof(OpenQA.Selenium.Opera.OperaOptions)) })
        {
            AddHostType(ref jSE, tso.name, tso.type);
        }
    }

还有另一种选择,就是把foreach子句改成

foreach (var tso in new[] {

这会创建一个隐式数组并鼓励编译器找出元素是什么。

【讨论】:

    猜你喜欢
    • 2012-10-12
    • 2018-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多