简体   繁体   English

如何使用 roslyn 获取未使用的引用?

[英]How to get unused references using roslyn?

I have hundreds of projects and I want to remove unused references from each project using roslyn.我有数百个项目,我想使用 roslyn 从每个项目中删除未使用的引用。 I saw this https://github.com/dotnet/roslyn/issues/625 but I have no Idea If it's already implemented and If there is a poc example that I can start with.我看到了这个https://github.com/dotnet/roslyn/issues/625但我不知道它是否已经实现,如果有一个我可以开始的 poc 示例。

Please help me If you have any idea how to do this.如果您有任何想法,请帮助我。

I did it by registering the compilation action that simply examines all the source trees in a project, finds all symbols referenced by them and considers the assemblies that declare them used, so everything else that's referenced by the project is unused.我通过注册编译操作来做到这一点,该操作仅检查项目中的所有源代码树,查找它们引用的所有符号并考虑声明它们使用的程序集,因此项目引用的所有其他内容都未使用。 Here's the analyzer: https://github.com/HellBrick/HellBrick.Diagnostics/blob/ff0b3a415475eda3d2e9927a2ab4638ada5d7148/src/HellBrick.Diagnostics/UnusedReferences/UnusedReferencesAnalyzer.cs这是分析器: https : //github.com/HellBrick/HellBrick.Diagnostics/blob/ff0b3a415475eda3d2e9927a2ab4638ada5d7148/src/HellBrick.Diagnostics/UnusedReferences/UnusedReferencesAnalyzer.cs

Not sure if this is the most clean and correct way to do this, but it seems to get the job done.不确定这是否是最干净和正确的方法,但它似乎完成了工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM