【问题标题】:How can I add a Source Generator as nuget reference?如何添加源生成器作为 nuget 参考?
【发布时间】:2023-03-25 09:14:01
【问题描述】:

如何将 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 包作为源生成器。

【问题讨论】:

  • 跟其他包一样添加就行了
  • @canton7 &lt;PackageReference Include="xyz.SourceGenerators" Version="1.0.0"&gt; 对我不起作用。生成器未执行
  • 然后发生了其他事情:您正确引用了它
  • stackoverflow.com/questions/65010453/…,可能对你有帮助
  • @AlexandruClonțea 你猜对了,问题是 nuget 包不正确。

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


【解决方案1】:

我找到了解决方案。感谢您在 cmets 中的提示。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多