How can I sort Fields, Properties and Methods in Visual Studio?

前端 未结 5 1376
迷失自我
迷失自我 2020-12-29 18:08

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?

相关标签:
5条回答
  • 2020-12-29 18:46

    Another option I found is XArrange. It is free and can be installed from the Extension Manager.

    Issues I took with it...

    1. It puts methods before constructors.
    2. It removes any comments between members,properties,methods, etc.
    3. It's configuration is very limited. No way to change sorting order.

    0 讨论(0)
  • 2020-12-29 18:55

    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. NArrange seems to be able to do this. No idea, never used it myself.

    0 讨论(0)
  • 2020-12-29 18:56

    If Code Sorting is your only concern, you can try the Visual Studio Extension 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.

    0 讨论(0)
  • 2020-12-29 18:57

    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 IDisposable with void Dispose() method in it) and methods that handle an event.

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

    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.

    Triggering

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

    1: ReSharper Type Members XSD Schema

    0 讨论(0)
  • 2020-12-29 19:00

    I think CodeMaid is the best free option for code formatting in Visual Studio.


    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

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

    0 讨论(0)
提交回复
热议问题