Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
.NET 6 Preview 7: Runtime errors with generic math

I'm not sure if I've discovered a bug or if I'm doing something wrong, but this issue is not listed in the known issues of .NET 6:

I've got a unit test project with a single file that looks like this:

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();
    }
}

The test fails because of the following runtime error:

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'.

But I very clearly have implemented the addition operator. In fact, when I build using dotnet build it compiles just fine until I remove the addition operator. Am I missing something?

Here is my project file:

<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>
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I was able to fix this error by moving Foo to a separate project and building that project using dotnet build from the command line. I then had to prevent VS from building the project again by referencing the dll directly from my test project.

My suspicion is that VS 2019 is not using the same compiler as dotnet build, and that the compiler in VS 2019 is failing to register the + operator as an interface member to IAdditionOperators.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda