排序和删除(未使用)using语句罗斯林脚本/代码? [英] Sort and remove (unused) using statements Roslyn script/code?

查看:219
本文介绍了排序和删除(未使用)using语句罗斯林脚本/代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using语句罗斯林脚本/代码排序和删除(未使用)?我在寻找,可以通过项目运行,并进行排序和删除使用语句未使用的部分.NET /罗斯林(编译器作为服务)的代码。我相信这是可能的罗斯林?任何人都可以点我的代码,可以做到这一点重写?


解决方案

这是在Visual Studio中的功能,但在学术上我想你将收集利用您的SyntaxTree声明是这样的:





<预类=郎-CS prettyprint-覆盖> VAR 。usings = syntaxTree.Root.DescendentNodes()式(节点是UsingDirectiveSyntax);



...和比较,通过符号表这样解决了命名空间:



<预类=郎-CS prettyprint-覆盖> 私有静态的IEnumerable< INamespaceSymbol> GetNamespaceSymbol(ISymbol符号)
{
如果(符号= NULL&放大器;!&安培;!symbol.ContainingNamespace = NULL)
收益率的回报symbol.ContainingNamespace;
}

变种纳秒= semanticModel.SyntaxTree.Root.DescendentNodes()的SelectMany(节点= GT;
GetNamespaceSymbol(semanticModel.GetSemanticInfo(节点).Symbol))。鲜明();


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?

解决方案

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();

这篇关于排序和删除(未使用)using语句罗斯林脚本/代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆