【问题标题】:Why do C# 7.2 features not compile in a UWP application?为什么 C# 7.2 功能无法在 UWP 应用程序中编译?
【发布时间】:2018-06-13 14:32:51
【问题描述】:

...特别是in (readonly ref) 参数。这是我的情况:

我在同一个 Visual Studio 解决方案中有一个 UWP 项目和一个 UWP 单元测试项目。这两个项目都针对 C# 7.2 主 UWP 项目有这个类(注意 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 项目中的相同用法不会产生任何编译器错误。

这是为什么呢?

【问题讨论】:

  • 那么您在Properties > Build > Advanced Build Settings 测试项目中也选择了C# 7.2?
  • @AndrésRobinet:测试项目真的需要更高的 C# 级别来针对已编译的项目运行测试吗?
  • @Andrés Robinet,是的,两者都针对 C# 7.2

标签: c# uwp c#-7.2 in-parameters


【解决方案1】:

在运算符中存在in 的编译器错误,从另一个项目/程序集加载运算符时会丢失。

https://github.com/dotnet/roslyn/pull/23508(修复将在 15.6 预览版 3 中发布)

https://github.com/dotnet/roslyn/issues/23689(此问题的另一份报告)

【讨论】:

  • 感谢@Julien Couvreur。我想知道(对你来说不是问题:)),这是否意味着没有人可以发布任何人都可以使用的 C# 7.2 编译程序集
  • @SteveDunn 我不确定,但我认为这种情况不会受到影响。
猜你喜欢
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多