2

如何将源生成器作为 nuget 包添加到 C# net5 项目?

要将项目添加为源生成器,请执行以下代码:

<ItemGroup>
  <ProjectReference Include="..\xyz.SourceGenerators\xyz.SourceGenerators.csproj"
    OutputItemType="Analyzer"
    ReferenceOutputAssembly="false"
    SetTargetFramework="TargetFramework=netstandard2.0" />
</ItemGroup>

但是我正在寻找 XMLPackageReference来添加一个 nuget 包作为源生成器。

4

1 回答 1

3

我制定了解决方案。感谢您在评论中的提示。

<PackageReference Include="xyz.SourceGenerators" Version="1.0.0">

添加一个包含源生成器的 Nuget 包就足够了。

但是,您需要将生成器正确添加到 nuget 包中。为此,将以下行添加到.csprojnuget 包中。

<ItemGroup>
  <None Include="$(OutputPath)\netstandard2.0\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>

解决方案来自:如何打包 C# 9 源代码生成器并将其上传到 Nuget?

于 2021-09-15T13:16:01.123 回答