C#源代码生成器-警告CS8032 : An instance of analyzer cannot be created

标签 c# .net code-generation c#-9.0

我正在尝试构建一个Source Generator。现在,只有最基本的静态方法返回“Hello World”。
生成器项目已构建,但是生成的代码不可用,调试器从不启动,并且构建输出显示

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


我引用的示例
  • Roslyn Team Generator Sample Project
  • Roslyn Team Generator Cookbook
  • Generator.Equals Project
  • How To Debug C# 9 Source Generators

  • 我试过了
  • 更改生成器和测试项目的TargetFramework和LanguageVersion
  • 引用了许多版本的分析器库Microsoft.CodeAnalysis.CSharpMicrosoft.CodeAnalysis.Analyzers
  • 引用Microsoft.Net.Compilers.Toolset的显式版本
  • 向NetStandard库
  • 添加显式引用
  • 从零开始,使用分析仪项目模板
  • 寻找一个生成器项目模板(但没有找到)

  • 版本号
    Visual Studio:版本16.8.3
    .NET SDK:5.0.101
    代码
    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>
    
    测试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]
        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
            }
        }
    

    最佳答案

    源生成器必须是.Net Standard 2.0才能在Visual Studio中运行。

    关于C#源代码生成器-警告CS8032 : An instance of analyzer cannot be created,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65479888/

    相关文章:

    c++ - 如何自动生成C++函数?

    inheritance - MAUI ContentView 无法从自定义基类继承

    c# - 为什么这个循环只有在我不调试的时候才会无限循环?

    c# - 未找到 Microsoft.web.Infrastructure.dll - Visual Studio 2015

    c# - c# 中的 HttpWebRequest 不适用于 .net 4.5

    c# - 网络服务中的接口(interface)

    .net - 如何使用 VBS 以编程方式将 DFS 路径转换为 ​​UNC?

    c# - 是否有编译器开关来关闭对 ​​C# 中泛型的支持?

    c# - .Net核心MySql

    java - UML 到 Java 工具?