visual-studio 如何在 Visual Studio 中对字段、属性和方法进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3326322/
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
How can I sort Fields, Properties and Methods in Visual Studio?
提问by Freshblood
I know that for clarity in codes, I have to write first fields then constructor and then methods etc. But how can I automate it in Visual Studio?
我知道为了代码的清晰性,我必须先编写字段,然后是构造函数,然后是方法等。但是如何在 Visual Studio 中实现自动化呢?
回答by Alan Feekery
I think CodeMaidis the best free option for code formatting in Visual Studio.
我认为CodeMaid是 Visual Studio 中代码格式化的最佳免费选择。
To sort your file, open the file via solution explorer:
要对文件进行排序,请通过解决方案资源管理器打开文件:
- Right click the open file
- Code Maid menu (likely near the top of the right click menu)
- Click
Reorganize Active Document
- 右键单击打开的文件
- Code Maid 菜单(可能在右键菜单顶部附近)
- 点击
Reorganize Active Document
Alternatively, using the default CodeMaid hotkeys CTRL+M,Zto sort your active file.
另外,使用默认热键CodeMaid CTRL+ M,Z整理你的活动文件。
回答by Matthias
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.
ReSharper 可以按名称、可访问性、类型、只读等对您的类成员(字段、构造函数、方法、委托)进行排序……您还可以用区域包围特定成员。我最喜欢的是将接口成员(例如,#region IDisposable其中包含void Dispose()方法)和处理事件的方法分组的能力。
ReSharper provides both - an easy way to configure and trigger the sorting of class members.
ReSharper 提供了两者 - 一种配置和触发类成员排序的简单方法。
Configuration
配置
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 the help of IntelliSense.
在 Visual Studio 中创建一个 XML 文件,并将默认类型成员布局(ReSharper 选项 > 语言 > C# > 类型成员布局)复制粘贴到该文件中。下载最新的XSD 架构。将架构文件添加到 Visual Studio 的架构文件(菜单 > XML > 架构... > 添加)。您现在应该能够在 IntelliSense 的帮助下编辑 XML 文件。
Triggering
触发
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 将自动重新排列您的班级成员。
回答by Hans Passant
Visual Studio has no feature that allows re-ordering already written code. That's the domain of add-ons. Resharper has a "Reordering type members" feature but that's only supported for C#, not for VB.NET code. NArrangeseems to be able to do this. No idea, never used it myself.
Visual Studio 没有允许重新排序已编写代码的功能。这就是附加组件的领域。Resharper 具有“重新排序类型成员”功能,但仅支持 C#,不支持 VB.NET 代码。 NArrange似乎能够做到这一点。不知道,自己没用过。
回答by SreekanthGS
If Code Sorting is your only concern, you can try the Visual Studio Extension CodeSorter
如果代码排序是您唯一关心的问题,您可以尝试 Visual Studio 扩展CodeSorter
CodeSorter is highly customizable extension that allows its users to sort C# code itby various conditions such as names, types (method, class, struct, ...), visibility modifiers (private, public, ...), constness modifiers (const, static, readonly) and virtuality modifiers (virtual, override, new, ...).
It is possible to assign multiple search criteria to have the same priority, for example place protected and private members first, and then publics and internals.
Names are compared in a way that leading "m_" and "_" is stripped. This allows fields and properties (eg. _foo and Foo) to be right next to each other.
One-lined declarations are placed without delimiting blank line, while multi-lined declarations are separated by a blank line. The exception is that one-lined field followed by a multi-lined property without comment of the same name will be placed without the delimiting blank line.
CodeSorter 是高度可定制的扩展,允许其用户根据名称、类型(方法、类、结构等)、可见性修饰符(私有、公共、...)、常量修饰符(const、 static、readonly)和虚拟修饰符(virtual、override、new、...)。
可以分配多个搜索条件以具有相同的优先级,例如首先放置受保护和私有成员,然后是公共和内部成员。
名称的比较方式是去除前导“m_”和“_”。这允许字段和属性(例如。_foo 和 Foo)彼此相邻。
单行声明放置时不分隔空行,而多行声明由空行分隔。例外情况是单行字段后跟多行属性而没有同名注释将被放置在没有分隔空行的情况下。
回答by Drew Chapin
Another option I found is XArrange. It is free and can be installed from the Extension Manager.
我发现的另一个选项是XArrange。它是免费的,可以从扩展管理器安装。
Issues I took with it...
我处理的问题...
- It puts methods before constructors.
- It removes any comments between members,properties,methods, etc.
It's configuration is very limited. No way to change sorting order.


