C#源生成器-警告CS8032:无法创建分析器的实例

c#

c#-9.0

我正在尝试构建一个源代码生成器。现在,只是返回“Hello World”的最基本的静态方法。

生成器项目构建,但生成的代码不可用,调试器从不启动,构建输出显示

CSC:警告 CS8032:无法从 ...binDebugnet5.0Generator.StaticPropertyEnum.dll 创建分析器 Generator.StaticPropertyEnum.helloWorld 的实例:无法加载文件或程序集“System.Runtime, Version=5.0” .0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一。该系统找不到指定的文件..

我参考的例子

  • Roslyn 团队生成器示例项目
  • Roslyn 团队生成器食谱
  • Generator.Equals 项目
  • 如何调试 C# 9 源代码生成器

我试过了

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

版本

Visual Studio:版本 16.8.3
.NET SDK:5.0.101

代码

生成器.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.StaticPropertyEnumGenerator.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 中运行。

  • Hmm. I'm sure I'd tried that. The build succeeded once I also downgraded `Microsoft.CodeAnalysis.CSharp` to 3.8.0. Intellisense still thinks it's broken though.
  • Ah. I had to delete the .vs folder
  • @farlee2121 - Finally! I'm very new to source generators and absolutely could not get it to work until I downgraded from 3.9.0 to 3.8.0. Very helpful tip, thanks!

以上是C#源生成器-警告CS8032:无法创建分析器的实例的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>