【问题标题】:.NET 6 Preview 7: Runtime errors with generic math.NET 6 Preview 7:通用数学的运行时错误
【发布时间】:2021-10-06 22:35:24
【问题描述】:

我不确定我是否发现了一个错误或者我做错了什么,但这个问题没有在 .NET 6 的known issues 中列出:

我有一个带有单个文件的单元测试项目,如下所示:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTest
{
    [TestClass]
    public class TestFoo
    {
        [TestMethod]
        public void Test()
        {
            var foo = new Foo();
        }
    }

    public class Foo : IAdditionOperators<Foo, Foo, Foo>
    {
        public static Foo operator +(Foo left, Foo right) => new();
    }
}

由于以下运行时错误,测试失败:

System.TypeLoadException: Virtual static method 'op_Addition' is not implemented on type 'UnitTest.Foo' from assembly 'UnitTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

但我很清楚地已经实现了加法运算符。事实上,当我使用dotnet build 构建时,它编译得很好,直到我删除了加法运算符。我错过了什么吗?

这是我的项目文件:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <EnablePreviewFeatures>true</EnablePreviewFeatures>
    <LangVersion>preview</LangVersion>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Runtime.Experimental" Version="6.0.0-preview.7.21377.19" />
  </ItemGroup>


  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
    <PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
    <PackageReference Include="coverlet.collector" Version="3.0.2" />
  </ItemGroup>

</Project>

【问题讨论】:

  • dotnet.microsoft.com/download/dotnet/6.0 "Visual Studio 支持 Visual Studio 2022 (v17.0 最新预览版) Visual Studio 2019 for Mac (v8.10)"。新的语言功能通常需要新版本的 Visual Studio。但这也是一个实验性的预览功能。所以YMMV。

标签: c# .net .net-6.0 preview-feature


【解决方案1】:

我可以通过将Foo 移动到一个单独的项目并从命令行使用dotnet build 构建该项目来修复此错误。然后我不得不通过直接从我的测试项目中引用 dll 来阻止 VS 再次构建项目。

我怀疑 VS 2019 没有使用与 dotnet build 相同的编译器,并且 VS 2019 中的编译器未能将 + 运算符注册为 IAdditionOperators 的接口成员。

【讨论】:

    猜你喜欢
    • 2021-11-26
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多