一次跨越整个项目或解决方案中删除未使用的命名空间 [英] Remove unused namespaces across a whole project or solution at once

查看:92
本文介绍了一次跨越整个项目或解决方案中删除未使用的命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以做到这一点通过的文件中。

I know you can do it file by file.

反正有没有做到这一点,在一个项目中的一个步骤,所有的文件?

Is there anyway to do this in one step for all files in a project?

推荐答案

你的意思是使用的语句?首先,请注意,他们一般做其他任何伤害的需要的空间。
类的工具 ReSharper的提供自动技巧来做到这一点,但是:有一个的link在VS供稿前阵子;它归结为:

Do you mean using statements? First, note that they generally do no harm other that take space. Tools like ReSharper offer automated tricks to do this, however: there was a link in the VS feed a little while ago; it boils down to:


  • 去工具 - >宏 - >宏IDE ...

  • 在Project Explorer中,添加 - >添加模块...(把一个名字 - 我用OrganiseUsings)

  • 下面
  • $ b $的代码粘贴在b
  • 文件 - >保存MyMacros,退出

  • go to Tools -> Macros -> Macros IDE...
  • in the Project Explorer, Add -> Add Module... (put in a name - I've used OrganiseUsings)
  • paste over with the code below
  • File -> Save MyMacros, exit

现在,如果你在工具栏上单击鼠标右键,自定义... - 你应该能够找到MyMacros.OrganiseUsings.RemoveAndSortAll - 拖动这个地方派上用场了。(也许在工具菜单中,你可能还需要将其放置后更改名称)

Now if you right-click on the toolbar and Customize... - you should be able to find MyMacros.OrganiseUsings.RemoveAndSortAll - drag this somewhere handy (maybe the Tools menu; you might also want to change the name after placing it).

您现在可以使用此选项来运行卸下并为整个解决方案sort命令。一个很大的节省时间。

You can now use this option to run the Remove and Sort command for an entire solution. A big time-saver.

==== ====代码

==== code ====

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module OrganiseUsings

    Public Sub RemoveAndSortAll()
        On Error Resume Next
        Dim sol As Solution = DTE.Solution

        For i As Integer = 1 To sol.Projects.Count    
            Dim proj As Project = sol.Projects.Item(i)    
            For j As Integer = 1 To proj.ProjectItems.Count    
                RemoveAndSortSome(proj.ProjectItems.Item(j))    
            Next    
        Next    
    End Sub    

    Private Sub RemoveAndSortSome(ByVal projectItem As ProjectItem)
        On Error Resume Next
        If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then    
            If projectItem.Name.LastIndexOf(".cs") = projectItem.Name.Length - 3 Then
                Dim window As Window = projectItem.Open(Constants.vsViewKindCode)

                window.Activate()

                projectItem.Document.DTE.ExecuteCommand("Edit.RemoveAndSort")

                window.Close(vsSaveChanges.vsSaveChangesYes)
            End If    
        End If    

        For i As Integer = 1 To projectItem.ProjectItems.Count    
            RemoveAndSortSome(projectItem.ProjectItems.Item(i))    
        Next
    End Sub   

End Module

这篇关于一次跨越整个项目或解决方案中删除未使用的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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