2016-12-02 176 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