2016-12-02 175 views
13

我刚刚升级到VS2017,但马上就关闭了,我的项目无法再生成,因为我收到了一堆当我使用VS15时不存在的奇怪编译器错误。Visual Studio 2017编译器错误

错误如:

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

编辑1:

  • 就创建了一个小型控制台应用程序,并复制了一些代码,并在相同的编译器错误(S)的出现
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 enter image description here
+6

你可以在一个小程序中重现错误? –

+3

它是否指定了一行代码“语法错误”应该是,如果是这样的行? –

+0

@MthetheWWatson#编辑 –

回答

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,因为通常您需要目标类型的变量。

+0

同样的问题与图案**是表达式使用时,会发生**泛型数组...另请参见[C#7.0中的新增功能](https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/)。 – Phil1970

+0

'as'运营商的工作,但没有理由'不'像往常一样工作。 –

+1

我同意它应该编译,但我怀疑没有单元测试需要**(1)**数组,**(2)**泛型和**(3)**'是'运算符。我建议您将代码简化为最简单的示例,然后将问题提交给Microsoft。 – Phil1970

1

C#7将is运营商从纯粹的型号测试扩展到他们所称的Pattern Matching

解析器现在似乎被is后接通用数组所迷惑。我想尝试与类型的parens,但我不能测试这种解决方案

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

括号不起作用... – Phil1970

+0

在这种情况下,带模式的Is表达式也不起作用:'if(array2D is Bgra [,] localName)'当单独使用泛型或数组时不会编译。 – Phil1970