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

前端 未结 5 1391
迷失自我
迷失自我 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: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

提交回复
热议问题