VS2019 IDE 和命令行编译器在 C# 语句上存在分歧

所以在 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]


命令行工具被调用为


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

以 RR 的csproj文件作为参数。我尝试向其提供文件sln,但没有效果。


解决办法是什么?


一只名叫tom的猫
浏览 94回答 2
2回答

慕勒3428872

我会将您的命名更改Tuple为以下命名 ValueTupleC# 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))),

FFIVE

该类型不喜欢成为一个对象,并坚持成为一个System.Type。&nbsp; &nbsp; public static void ConnectToOperaObjects(ref Microsoft.ClearScript.Windows.JScriptEngine jSE)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; foreach (var tso in new List<(string name, System.Type type)>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ("CSOperaDriver", typeof(OpenQA.Selenium.Opera.OperaDriver)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ("CSOperaDriverService", typeof(OpenQA.Selenium.Opera.OperaDriverService)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ("CSOperaOptions", typeof(OpenQA.Selenium.Opera.OperaOptions)) })&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AddHostType(ref jSE, tso.name, tso.type);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }还有另一种选择,即将 foreach 子句更改为foreach (var tso in new[] {这会创建一个隐式数组并鼓励编译器找出元素是什么。
打开App,查看更多内容
随时随地看视频慕课网APP