【发布时间】:2016-12-02 15:47:34
【问题描述】:
我刚刚升级到 VS2017,但我的项目马上无法构建,因为我遇到了一堆奇怪的编译器错误,而这些错误在我使用 VS15 时并不存在。
错误如:
Syntax Error; value expectedInvalid 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:
【问题讨论】:
-
能否用小程序重现错误?
-
它是否指定了“语法错误”应该是的代码行,如果是,那是什么行?
-
@MatthewWatson #Edited
-
尝试 as 关键字并检查是否为空。 var tmp = array2D as Bgra
[,]; if (tmp != null) (这里没有 VS2017,所以我无法测试) -
看起来像是编译器中的一个重大变化,也可能是一个错误。我可以重现这个问题(而且在以前的编译器版本中肯定可以正常工作!)
标签: c# visual-studio-2017