如何将源代码生成器作为 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/