本文介绍了Visual Studio扩展:如何按汇编排序使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认"删除并排序使用"命令只能按字母顺序排序。并且它删除了未使用的未使用的使用。


所以,我想按顺序进行扩展排序:系统,.Net程序集, 其他程序集和我的项目。


现在我写了下一篇:

 ProjectItem projectItem = dte2.ActiveDocument.ProjectItem; 
FileCodeModel2 fileCodeModel =(FileCodeModel2)projectItem.FileCodeModel;
var imports = Flatten(fileCodeModel.CodeElements).Where(i => i.Kind == vsCMElement.vsCMElementImportStmt).Cast< CodeImport>(); //所有当前文档的导入


VSProject vsProject =(VSProject)projectItem.ContainingProject.Object;
参考[] references = vsProject.References.Cast< Reference>()
.OrderBy(i =>!i.Path.Contains(" \\.NETFramework\\) ;))
.ThenBy(i =>!i.Path.Contains(" \\UnityAssemblies\\")))
.ThenBy(i =>!i。 Path.Contains(" \\Plugins \\"))
.ThenBy(i => i.Path).ToArray(); //当前项目的所有依赖项

然后我可以使用ReflectionOnlyLoad来获取所有类型和名称空间。并制作所有命名空间的有序列表,然后通过此列表对文档的命名空间进行排序。 

但我认为Visual Studio应该知道所有类型和命名空间。我对吗?我可以访问这些数据吗?


或者还有其他建议吗?


解决方案

Default "Remove And Sort Usings" command can only sort by alphabetical order. And it removes unused usings that is bad.

So, I want to make an extension sorting in order: System, .Net assemblies,  other assemblies and my projects.

Now I wrote the next:

ProjectItem projectItem = dte2.ActiveDocument.ProjectItem;
FileCodeModel2 fileCodeModel = (FileCodeModel2) projectItem.FileCodeModel;
var imports = Flatten( fileCodeModel.CodeElements ).Where( i => i.Kind == vsCMElement.vsCMElementImportStmt ).Cast<CodeImport>(); // all imports of current document


VSProject vsProject = (VSProject) projectItem.ContainingProject.Object;
Reference[] references = vsProject.References.Cast<Reference>()
.OrderBy( i => !i.Path.Contains( "\\.NETFramework\\" ) )
.ThenBy( i => !i.Path.Contains( "\\UnityAssemblies\\" ) )
.ThenBy( i => !i.Path.Contains( "\\Plugins\\" ) )
.ThenBy( i => i.Path ).ToArray(); // all dependencies of current project

Then I could use ReflectionOnlyLoad to get all types and namespaces. And to make ordered list of all namespaces and then sort document's namespaces via this list. 
But I think Visual Studio should know about all types and namespaces. Am I right? Can I access those data?

Or are there any other suggestions?

解决方案


这篇关于Visual Studio扩展:如何按汇编排序使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 13:14