为什么 C# 7.2 功能不能在 UWP 应用程序中编译?
Why do C# 7.2 features not compile in a UWP application?
... 特别是 in
(readonly ref
) 参数。这是我的情况:
我在同一 Visual Studio 解决方案中有一个 UWP 项目和一个 UWP 单元测试项目。这两个项目都以 C# 7.2 为目标主要的 UWP 项目有这个 class(注意 in
参数):
public struct Cell
{
public Cell(int x, int y)
{
X = x;
Y = y;
}
public int X { get; }
public int Y { get; }
public static Cell operator +(in Cell left, in Cell right)
{
return new Cell(left.X + right.X, left.Y + right.Y);
}
public static Cell operator -(in Cell left, in Cell right)
{
return new Cell(left.X - right.X, left.Y - right.Y);
}
public override string ToString() => $"{X}, {Y}";
}
当我使用来自 UWP 测试项目的那些运算符时:
[TestMethod]
public void TestMethod1()
{
Cell cell1 = new Cell(0, 0);
Cell cell2 = new Cell(1, 1);
var added = cell1 + cell2 ;
var minus = cell1 - cell2 ;
}
我得到:
UnitTest.cs(16,25,16,38): error CS0019: Operator '+' cannot be applied to operands of type 'Cell' and 'Cell'
UnitTest.cs(17,25,17,38): error CS0019: Operator '-' cannot be applied to operands of type 'Cell' and 'Cell'
但是,在主 UWP 项目中使用相同的用法不会产生任何编译器错误。
这是为什么?
运算符中存在 in
的编译器错误,当从另一个 project/assembly 加载运算符时,它会丢失。
https://github.com/dotnet/roslyn/pull/23508(修复将在 15.6 预览版 3 中发布)
... 特别是 in
(readonly ref
) 参数。这是我的情况:
我在同一 Visual Studio 解决方案中有一个 UWP 项目和一个 UWP 单元测试项目。这两个项目都以 C# 7.2 为目标主要的 UWP 项目有这个 class(注意 in
参数):
public struct Cell
{
public Cell(int x, int y)
{
X = x;
Y = y;
}
public int X { get; }
public int Y { get; }
public static Cell operator +(in Cell left, in Cell right)
{
return new Cell(left.X + right.X, left.Y + right.Y);
}
public static Cell operator -(in Cell left, in Cell right)
{
return new Cell(left.X - right.X, left.Y - right.Y);
}
public override string ToString() => $"{X}, {Y}";
}
当我使用来自 UWP 测试项目的那些运算符时:
[TestMethod]
public void TestMethod1()
{
Cell cell1 = new Cell(0, 0);
Cell cell2 = new Cell(1, 1);
var added = cell1 + cell2 ;
var minus = cell1 - cell2 ;
}
我得到:
UnitTest.cs(16,25,16,38): error CS0019: Operator '+' cannot be applied to operands of type 'Cell' and 'Cell'
UnitTest.cs(17,25,17,38): error CS0019: Operator '-' cannot be applied to operands of type 'Cell' and 'Cell'
但是,在主 UWP 项目中使用相同的用法不会产生任何编译器错误。
这是为什么?
运算符中存在 in
的编译器错误,当从另一个 project/assembly 加载运算符时,它会丢失。
https://github.com/dotnet/roslyn/pull/23508(修复将在 15.6 预览版 3 中发布)