How do I sort my code (by method name) in Visual S

2020-07-03 07:23发布

Short of cutting and pasting, is there a way to sort the methods in my classes in Visual Studio 2008? I like orderly code.

5条回答
我欲成王,谁敢阻挡
2楼-- · 2020-07-03 07:32

This is a free plug-in that does what you are asking: http://www.visualstudiogallery.com/ExtensionDetails.aspx?ExtensionID=800978aa-2aac-4440-8bdf-6d1a76a5c23c

Update

Unfortunately the link is outdated. You can download Regionerate at http://www.rauchy.net/regionerate/docs/2007/05/download.html

查看更多
姐就是有狂的资本
3楼-- · 2020-07-03 07:37

Resharper will do a good job in a limited way. It depends on how much you want. For example, it wont go and reorder your overrides in an asp.net page based on lifecycle, or anything like that, but it will keep properties, fields, methods and what not clearly grouped

EDIT: By the eway i was refering to auto reordering aka reformatting.

查看更多
老娘就宠你
4楼-- · 2020-07-03 07:39

ReSharper has Code Reordering functionality and a File Structure view that lets you do drag and drop reordering.

查看更多
祖国的老花朵
5楼-- · 2020-07-03 07:47

You may find or be able to make a macro to do this, but there is no built in functionality of VS to sort your methods. Some third party productivity tools like Resharper and CodeRush provide some functionality to reorder your code.

查看更多
够拽才男人
6楼-- · 2020-07-03 07:54

If you are using Resharper, you can change the Type Members Layout template so that it orders your code however you like. See under Resharper>Options>Languages>C#>Type Members Layout. alt text http://www.jetbrains.com/resharper/features/screenshots/40/automatic_member_layout_full.png

You can, for example, put methods with particular attributes first in your file... e.g. methods marked with NUnit's [Setup] and [TearDown] could come before methods marked with [Test] by placing a block like:

<!--Fixture Setup/Teardown-->
<Entry>
  <Match>
    <And>
      <Kind Is="method"/>
      <Or>
        <HasAttribute CLRName="NUnit.Framework.TestFixtureSetUpAttribute" Inherit="true"/>
        <HasAttribute CLRName="NUnit.Framework.TestFixtureTearDownAttribute" Inherit="true"/>
      </Or>
    </And>
  </Match>
 </Entry>

before:

<!--Test methods-->
<Entry>
  <Match>
    <And Weight="100">
      <Kind Is="method"/>
      <HasAttribute CLRName="NUnit.Framework.TestAttribute" Inherit="false"/>
    </And>
  </Match>
  <Sort>
    <Name/>
  </Sort>
</Entry>

and then have a catch-all for everything else:

<!--All other members-->
<Entry>
  <Sort>
    <Name/>
  </Sort>
</Entry>

The template system is very powerful and should meet your needs.

查看更多
登录 后发表回答