【问题标题】:Visual Studio 2017 Compiler Error(s)Visual Studio 2017 编译器错误
【发布时间】:2016-12-02 15:47:34
【问题描述】:

我刚刚升级到 VS2017,但我的项目马上无法构建,因为我遇到了一堆奇怪的编译器错误,而这些错误在我使用 VS15 时并不存在。

错误如:

  • Syntax Error; value expected
  • Invalid Expression Term '['
  • Invalid Expression Term 'byte'
  • Using the generic type requires 1 type arguments

编辑 1:

  • 刚刚创建了一个小型控制台应用程序并将一些代码复制到其中,并且出现了相同的编译器错误
using System;
using System.Runtime.InteropServices;

namespace Error
{
    class Program
    {
        static void Main()
        {
            Array array2D = null;
            if (array2D is Bgra <byte>[,])
            {
            }
        }
    }

    public interface IColor { }

    public interface IColor<T> : IColor
        where T : struct
    { }

    public interface IColor2 : IColor { }

    public interface IColor2<T> : IColor2, IColor<T>
        where T : struct
    { }

    public interface IColor3 : IColor { }

    public interface IColor3<T> : IColor3, IColor<T>
        where T : struct
    { }

    public interface IColor4 : IColor { }

    public interface IColor4<T> : IColor4, IColor<T>
        where T : struct
    { }

    [StructLayout(LayoutKind.Sequential)]
    public struct Bgra<T> : IColor4<T>
        where T : struct
    {
        public Bgra(T b, T g, T r, T a)
        {
            B = b;
            G = g;
            R = r;
            A = a;
        }

        public T B;

        public T G;

        public T R;

        public T A;

        public override string ToString()
        {
            return $"B: {B}, G: {G}, R: {R}, A: {A}";
        }

        public const int IDX_B = 0;

        public const int IDX_G = 1;

        public const int IDX_R = 2;

        public const int IDX_A = 3;
    }
}

请注意,完全相同的项目在 VS15 甚至 VS13 中都可以编译。

编辑 2:

  • VS15 VS17

【问题讨论】:

  • 能否用小程序重现错误?
  • 它是否指定了“语法错误”应该是的代码行,如果是,那是什么行?
  • @MatthewWatson #Edited
  • 尝试 as 关键字并检查是否为空。 var tmp = array2D as Bgra[,]; if (tmp != null) (这里没有 VS2017,所以我无法测试)
  • 看起来像是编译器中的一个重大变化,也可能是一个错误。我可以重现这个问题(而且在以前的编译器版本中肯定可以正常工作!)

标签: c# visual-studio-2017


【解决方案1】:

根据我的测试,使用 as 运算符在 Visual Studio 2017 中按预期工作。

所以你可以使用

var tmp = array2D as Bgra<Byte>[,]; 
if (tmp != null) { ... }

另外,is 运算符也适用于简单数组:

if (array2D is int[,]) { ... }

也可以编译。

因此,如果您有一个泛型数组,这似乎是有问题的情况。事实上,如果你做类似的事情

if (array2D is List<int>[,]) { ... }

你会得到编译错误。

下面的代码也可以编译

object something = null;
if (something is List<int>) { ... }

因此,唯一有问题的情况是使用泛型类型的数组作为is 运算符的参数时。

附带说明一下,我通常更喜欢使用 as 运算符而不是 is 运算符,因为通常无论如何您都需要目标类型中的变量。

【讨论】:

  • 在使用泛型数组时,Is-expressions with patterns 也会出现同样的问题...另见What’s New in C# 7.0
  • as 操作员可以正常工作,但is 没有理由不正常工作。
  • 我同意它应该编译,但我怀疑对于需要 (1) 数组、(2) 的特定案例没有单元测试泛型和 (3) is 运算符。我建议您将代码简化为最小示例并将问题提交给 Microsoft。
【解决方案2】:

C#7 将 is 运算符从纯类型测试扩展到他们所谓的 Pattern Matching

解析器现在似乎被is 和array-of-generic 弄糊涂了。我会尝试在类型周围使用括号,但我无法测试此解决方案

if (array2D is (Bgra<byte>[,]))

【讨论】:

  • 在这种情况下,带有模式的 Is 表达式也不起作用:if (array2D is Bgra&lt;byte&gt;[,] localName) 也无法编译,而单独的泛型或数组可以正常工作。
猜你喜欢
  • 2018-12-08
  • 2017-08-28
  • 2018-05-26
  • 2019-04-01
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
相关资源
最近更新 更多