簡體   English   中英

Visual Studio 2017編譯器錯誤

[英]Visual Studio 2017 Compiler Error(s)

我剛剛升級到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 在此輸入圖像描述

根據我的測試,使用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運營商因為通常你需要在目標類型的變量反正。

C#7將is運算符從純類型測試擴展到他們所謂的模式匹配

解析器現在似乎被眼花繚亂is后跟陣列的泛型。 我試着用這種類型的parens,但我無法測試這個解決方案

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM