Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
.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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda