Sort and remove (unused) using statements Roslyn script/code?(使用语句 Roslyn 脚本/代码排序和删除(未使用)?)
问题描述
使用语句 Roslyn 脚本/代码排序和删除(未使用)?我正在寻找一些 .NET/Roslyn(编译器即服务)代码,它们可以在项目中运行并对未使用的 using 语句进行排序和删除.我相信罗斯林有可能吗?谁能指点我可以重写的代码?
Sort and remove (unused) using statements Roslyn script/code? I'm looking for some .NET/Roslyn (compiler as service) code that can run through a project and sort and remove unused using statements. I believe this is possible with Roslyn? Can anyone point me to code that could do this rewrite?
推荐答案
这是 Visual Studio 中的一个功能,但在学术上我认为你会从 SyntaxTree 中收集 using 语句,如下所示:
This is a feature in Visual Studio, but academically I think you would collect using statements from your SyntaxTree like this:
var usings = syntaxTree.Root.DescendentNodes().Where(node is UsingDirectiveSyntax);
...并将其与符号表解析的命名空间进行比较,如下所示:
...and compare that to the namespaces resolved by the symbol table like this:
private static IEnumerable<INamespaceSymbol> GetNamespaceSymbol(ISymbol symbol)
{
if (symbol != null && symbol.ContainingNamespace != null)
yield return symbol.ContainingNamespace;
}
var ns = semanticModel.SyntaxTree.Root.DescendentNodes().SelectMany(node =>
GetNamespaceSymbol(semanticModel.GetSemanticInfo(node).Symbol)).Distinct();
这篇关于使用语句 Roslyn 脚本/代码排序和删除(未使用)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用语句 Roslyn 脚本/代码排序和删除(未使用)?


- 如何用自己压缩一个 IEnumerable 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04