我不确定我是否发现了一个错误或者我做错了什么,但是这个问题没有在.NET 6的已知问题中列出:
我有一个带有单个文件的单元测试项目,如下所示:
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>