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

2.3K
Vistas
C# Source Generator - warning CS8032: An instance of analyzer cannot be created

I'm trying to build a Source Generator. Right now, just the most basic static method that returns "Hello World".

The generator project builds, but the generated code is not available, the debugger never starts, and the build output shows

CSC : warning CS8032: An instance of analyzer Generator.StaticPropertyEnum.helloWorld cannot be created from ...\bin\Debug\net5.0\Generator.StaticPropertyEnum.dll : Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified..

Examples I'm referencing

  • Roslyn Team Generator Sample Project
  • Roslyn Team Generator Cookbook
  • Generator.Equals Project
  • How To Debug C# 9 Source Generators

I've tried

  • changing the TargetFramework and LanguageVersion of both the generator and test projects
  • referencing many version of the analyzer libraries Microsoft.CodeAnalysis.CSharp and Microsoft.CodeAnalysis.Analyzers
  • referencing an explicit version of Microsoft.Net.Compilers.Toolset
  • Adding an explicit reference to the NetStandard library
  • starting from scratch with an analyzer project template
  • looking for a generator project template (but didn't find one)

Versions

Visual Studio: version 16.8.3
.NET SDK: 5.0.101

Code

Generator.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0-2.final" PrivateAssets="all" />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.0.0" PrivateAssets="all" />
  </ItemGroup>
 
</Project>

Test csproj

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

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="1.3.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Generator.StaticPropertyEnum\Generator.StaticPropertyEnum.csproj"  OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
  </ItemGroup>

</Project>

Generator

    [Generator]
    public class helloWorld : ISourceGenerator
    {
        public void Execute(GeneratorExecutionContext context)
        {


            context.AddSource("HelloWorld-generated.cs", @"
            using System;
            namespace HelloWorld
            {
                public static class Hello
                {
                    public static string SayHello() {
                        return ""HelloWorld"";
                    }
                }
            }");
        }

        public void Initialize(GeneratorInitializationContext context)
        {
#if DEBUG
            if(!Debugger.IsAttached) Debugger.Launch();
#endif
        }
    }
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Source Generators must be .NET Standard 2.0 to run in Visual Studio 2019+ or .NET Standard 1.x to run in Visual Studio 2017+.

over 4 years ago · Santiago Trujillo Denunciar

0

as @Yair-Halberstadt mentioned in a different answer, the specific error listed is due to the fact that currently source generator projects need to target netstandard2.0. However if you get the CS8032 error with a different assembly (for example Microsoft.CodeAnalysis, Version=3.0.x ...) your problem is probably caused by a SDK version mismatch. For example on my computer I have SDK 5.0.302 which has version 3.10.0 of Microsoft.CodeAnalysis and Microsoft.CodeAnalysis.CSharp. While the Generator project uses nuget to get these packages, the build of the project referencing the generator resolves these files from the SDK. This is why reverting to 3.8.0 has worked for some commentators, the SDK version they have installed contains 3.8.0 of these references.

over 4 years ago · Santiago Trujillo Denunciar

0

I have source generator that targets netstandard2.0 and net5.0 for nullability support.

 <TargetFrameworks>net5.0;netstandard2.0</TargetFrameworks>
 <Nullable>enable</Nullable>

and sample library that targets same frameworks.

It crashes when project is being built within Visual Studio, but builds ok from the terminal.

To solve this, I've changed target framework when referencing it as a generator with SetTagetFramework and now it compiles without any warnings or errors.

  <ItemGroup>
    <ProjectReference Include="..\MyGenerator\MyGenerator.csproj"
                      OutputItemType="Analyzer" ReferenceOutputAssembly="false"
                      SetTargetFramework="TargetFramework=netstandard2.0" />
  </ItemGroup>
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