visual-studio Visual Studio 中的按字母顺序排列的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/466417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:04:59  来源:igfitidea点击:

Alphabetizing methods in Visual Studio

visual-studiovisual-studio-2008coding-styleresharperorganization

提问by 80bower

Is there any sort of plug-in or tool available for Visual Studio 2008 to alphabetize methods? Ideally I'd like a tool that will alphabetize a selection, or specified type (i.e. only methods, not member variables), either automatically or on-demand.

是否有任何类型的插件或工具可用于 Visual Studio 2008 来按字母顺序排列方法?理想情况下,我想要一个工具,可以自动或按需按字母顺序排列选择或指定类型(即只有方法,而不是成员变量)。

采纳答案by DavidN

Resharperhas a Type Members Layout, which can order members by type, accessibility and alphabetically as well.

Resharper有一个类型成员布局,它可以按类型、可访问性和字母顺序对成员进行排序。

You can also take a look into Ora, which presents a pane in visual studio that is ordered (even though your source may not be).Link's dead.

您还可以查看Ora,它在 Visual Studio 中显示了一个已订购的窗格(即使您的源可能不是)。林克死了。

回答by Matthias

The following answer goes much further than the OP asks, because I believe, that sorting methods just by name is far not enough for most programmers. Mostly you want your methods, constructors, fields, event handlers, and interface implementations to be seperated from each other (i.e. through regions), and sorted by various criteria.

以下答案比 OP 所要求的更进一步,因为我相信,仅按名称对方法进行排序对于大多数程序员来说是远远不够的。大多数情况下,您希望您的方法、构造函数、字段、事件处理程序和接口实现彼此分开(即通过区域),并按各种标准进行排序。

For this purpose I tried NArrange, Regionerate, and others, but I've found them not intuitive to use. So ReSharperbecame the tool of my choice.

为此,我尝试了NArrangeRegionerate等,但我发现它们使用起来并不直观。所以ReSharper成为我选择的工具。

As already mentioned by @DavidN, ReSharper can sort your class members (fields, constructors, methods, delegates) by name, accessibility, type, readonly, etc... You can also surround specific members with regions. What I like the most is the ability to group interface members (e.g., #region IDisposablewith void Dispose()method in it) and methods that handle an event.

正如@DavidN 已经提到的,ReSharper 可以按名称、可访问性、类型、只读等对您的类成员(字段、构造函数、方法、委托)进行排序......您还可以用区域包围特定成员。我最喜欢的是将接口成员(例如,#region IDisposable其中包含void Dispose()方法)和处理事件的方法分组的能力。

ReSharper provides both - an easy way to configure and trigger the sorting of class members.

ReSharper 提供了两者 - 一种配置和触发类成员排序的简单方法。

Configuring the layout

配置布局

Create a XML file within Visual Studio and copy-paste the default type member layout (ReSharper Options > Languages > C# > Type Members Layout) into that file. Download the latest XSD schema. Add the schema file to Visual Studio's schema files (Menu > XML > Schemas... > Add). You should be able now, to edit the XML file with IntelliSense support.

在 Visual Studio 中创建一个 XML 文件,并将默认类型成员布局(ReSharper 选项 > 语言 > C# > 类型成员布局)复制粘贴到该文件中。下载最新的XSD 架构。将架构文件添加到 Visual Studio 的架构文件(菜单 > XML > 架构... > 添加)。您现在应该能够编辑具有 IntelliSense 支持的 XML 文件。

Triggering the reorder

触发重新排序

If you use the Visual Studiokeyboard scheme (ReSharper Options > Visual Studio Integration) and press Ctrl+E,Ffor Silent Code Cleanup. A dialog will pop up, where you can select a Code Cleanupsetting. For this setting you should check Reorder type members. The second time you press the shortcut, ReSharper will automatically reorder your class members.

如果您使用Visual Studio的键盘方案(ReSharper的选项> Visual Studio集成),并按下Ctrl+E,F静音代码清理。将弹出一个对话框,您可以在其中选择代码清理设置。对于此设置,您应该检查重新排序类型成员。第二次按下快捷键时,ReSharper 将自动重新排列您的班级成员。

1: ReSharper Type Members XSD Schema

1ReSharper 类型成员 XSD 架构

回答by Gabriel Andrés Brancolini

While Resharper has many cool features it has a large impact in CPU and I/O usage and can be very complicated to use. It is also only available under commercial licensing unless you qualify for a few very specific free use licenses.

虽然 Resharper 有许多很酷的功能,但它对 CPU 和 I/O 的使用有很大的影响,并且使用起来可能非常复杂。它也只能在商业许可下使用,除非您有资格获得一些非常具体的免费使用许可。

Try CodeMaid. It is free for commercial use and has a much lower performance overhead. I find it easy to use and it is very good for alphabetizing methods.

试试CodeMaid。它可免费用于商业用途,并且性能开销要低得多。我发现它易于使用,并且非常适合按字母顺序排列的方法。



To sort your file, open the file via solution explorer:

要对文件进行排序,请通过解决方案资源管理器打开文件:

  1. Right click the open file
  2. Code Maid menu (likely near the top of the right click menu)
  3. Click Reorganize Active Document
  1. 右键单击打开的文件
  2. Code Maid 菜单(可能在右键菜单顶部附近)
  3. 点击 Reorganize Active Document

Alternatively, using the default CodeMaid hotkeys CTRL+M,Zto sort your active file.

另外,使用默认热键CodeMaid CTRL+ MZ整理你的活动文件。

回答by Agent_9191

For C# you can use Regionerateto organize your code. You can create a template that simply organizes your code but doesn't use regions.

对于 C#,您可以使用Regionerate来组织您的代码。您可以创建一个模板来简单地组织您的代码但不使用区域。

回答by Jim Anderson

You can create such a function yourself using Devexpress' free DXCoreproduct (the same extensibility framework used to create CodeRush).

您可以使用Devexpress的免费DXCore产品(与用于创建CodeRush 的可扩展性框架相同)自己创建这样的函数。