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 脚本/代码排序和删除(未使用)?


基础教程推荐
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01