c# - How can I make a Roslyn Analyzer project a transitive dependency?
问题描述
I have a library that relies on a source generator to work correctly. In the MyLibrary.csproj
, I reference the generator like so.
<ItemGroup>
<ProjectReference
Include="..\MyLibrary.Generators\MyLibrary.Generators.csproj"
PrivateAssets="contentfiles;build"
ReferenceOutputAssembly="false"
OutputItemType="analyzer"/>
</ItemGroup>
I need this analyzer reference to be transitive, i.e. projects that reference MyLibrary
should get the MyLibrary.Generators
analyzer transitively.
A simple reference like so does not seem to reference the analyzer, only MyLibrary
<ProjectReference Include="..\MyLibrary\MyLibrary.csproj" />
I want to stress that I am not looking for MyLibrary.Generators
to be consumed as a regular assembly reference, but as a Roslyn Analyzer so my source generator can run as intended during compile time.
解决方案
我在GitHub 上收到了这个问题的答案。
库项目具有对生成器项目的以下引用就足够了:
<ItemGroup>
<!-- Package the generator in the analyzer directory of the nuget package -->
<None Remove="$(OutputPath)/$(AssemblyName).Generator.dll" />
<None Include="$(OutputPath)/$(AssemblyName).Generator.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>
(我不确定 Remove 行是否有任何作用。这可能是不必要的。)
当库作为 NuGet 包使用时,生成器会自动作为分析器包含在内,即分析器引用是可传递的。
但是,当通过项目引用(例如在库自己的单元测试项目中)使用库时,我们需要以下内容:
<ItemGroup>
<ProjectReference Include="..\MyLibrary\MyLibrary.csproj" />
<ProjectReference Include="..\MyLibrary.Generator\MyLibrary.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
原始帖子中的示例都处理项目引用。不幸的是,从我收集到的信息来看,对分析器的传递项目引用似乎是不可能的。
推荐阅读
- python - 如何检查 Pandas 列是否具有字符串列表中的值?
- android - 在android上导入nativescript-local-notifications错误
- html - 用于电子邮件模板的内联 CSS、@media 标记解决方案
- r - 使用 R 从网站下载文件不起作用
- java - 当我将 JAR 文件添加到 Intellij Idea 时为空库
- node.js - 节点js路由不起作用在浏览器上出现错误
- android - 单击按钮检查互联网连接
- python-3.x - 为什么这个函数参数在每次调用中都是相同的,尽管传递了不同的值?(在循环中创建闭包)
- python - 此功能的解决方案是什么
- c# - 无法在 pdf 中显示马拉地语文本