c# - 如何添加源生成器作为 nuget 引用?

标签 c# .net-5 sourcegenerators csharp-source-generator

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

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

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

但是我正在寻找 XML PackageReference 以将 nuget 包添加为源生成器。

最佳答案

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

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

足以添加包含源代码生成器的 Nuget 包。

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

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

解决方案来自:How to pack a C# 9 source generator and upload it to the Nuget?

关于c# - 如何添加源生成器作为 nuget 引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69165175/

相关文章:

c# - 是否可以在 Azure Functions .Net 5 中使用多个输出?

c# - VS 和 Rider 忽略源生成器生成的文件

c# - 源生成: How to get involved types from InvocationExpressionSyntax

c# - 如何在 GridView 中拥有具有相同字段但不同值的两列

azure - Azure 应用服务中的 .NET5 Blazor 服务器应用返回 "You do not have permission to view this directory or page"

angular - .net 5 - 信号器 - 消息包 - lz4 - 错误 : unable to find ext type 98

c# - div 中样式为 ="display:none"的 .NET 按钮未触发

c# - Windows 服务使用队列中的 Azure 服务总线消息

c# - 如何从 Python 文件句柄打开 .NET FileStream 对象?