Home > Coding Helpers, Shared Source > Organizing source code and sorting class members using CodeRush

Organizing source code and sorting class members using CodeRush

April 14th, 2011

Reorganization of the code is a great practice to improve code readability. If you prefer to sort your type members like this:

class {
private fields
public properties
public methods
private methods

or any other order, you can use the Member Organizer CodeRush plug-in for this purpose.

The plug-in is an open source DXCore plug-in from the CodeRush Shared Source solution. It contains only a single action which is not bound to any key, by default. To assign a key, see the appropriate topic – the command name is the “Organize Members“.

Because the plug-in has the source code, you are welcome to tweak it in any way you’d like. By default, when you press the shortcut key bound to the “Organize Members” command, it will take the current type declaration, where the editor caret is located, and organize its members using the following structure:

  • First, it sorts all members into groups by its visibility:
    • public
    • protected
    • protected internal
    • internal
    • private

Each one of these groups is automatically wrapped into a region (optional) with the corresponding name, for example:

#region Public members…


#region Private members…

  • Then, inside each visibility group, Member Organizerwill sort them by member type in the following order:
    • methods
    • properties
    • events
    • fields

Members inside such groups are preceded with a corresponding comment (optional), such as:

// Properties…
// Methods…

For example, having a code like this:

CodeRush Member Organizer (CS, before)

Show Visual Basic code… »

CodeRush Member Organizer (VB, before)

Member Organizer with default settings will reorganize the code into this:

CodeRush Member Organizer (CS, after)

Show Visual Basic code… »

CodeRush Member Organizer (VB, after)

To modify the order of members, you have to modify the source code of the plug-in and then recompile it. Open up the “MemberOrgPlugIn.cs” file, and go to the “MemberOrgPlugIn” class constructor. Here’s a part of its code:

_Filters = new FilterCollection();

The AddMainVisibiltyGroup method looks like this:

private void AddMainVisibiltyGroup(MemberVisibility visibility)
  VisibilityFilter newFilter = new VisibilityFilter(visibility);
  newFilter.Wrapper = WrapperMode.Region;
  AddSubFilter(newFilter, LanguageElementType.Method);
  AddSubFilter(newFilter, LanguageElementType.Property);
  AddSubFilter(newFilter, LanguageElementType.Event);
  AddSubFilter(newFilter, LanguageElementType.Variable);

And, finally, the AddSubFilter method:

private void AddSubFilter(VisibilityFilter newFilter, LanguageElementType elementType)
  MemberTypeFilter subFilter = new MemberTypeFilter(elementType);
  subFilter.Wrapper = WrapperMode.Comment;

As you can see, it is easy to modify the algorithm of the method sort. You can change positions of the members and specify the optional Wrapper mode, which can have three self-explained states:

  • None
  • Region
  • Comment

The latest version of the plug-in is attached with a few minor fixes (such as comment names). Don’t forget to specify the correct build path in the Project options of the plug-in.

Products: CodeRush Pro
Versions: 11.1 and up
VS IDEs: any
Updated: Nov/16/2011
ID: C086

Similar Posts:

  1. Marino Della Puppa
    October 11th, 2012 at 00:31 | #1

    wonderful plugin.
    Yet I have some problem customizing the order.
    As far as I understood, changing the order in which members are grouped is only a matter of changing the order of the lines of code in constructor and the in the method AddMainVisibilityGroup.
    (i.e. I want Events, Variables, Properties and Methods, and Private members before public ones).
    I’ve modified the code, compile it without any problems, but when I lanch it all I get is the default behaviour.

    (I checked the output directory and it’s correctly C:\Program Files (x86)\DevExpress 2010.2\IDETools\System\bin\PlugIns, and I have no other CR_MemberOrg.dll on my disk)

    Any help will be appreciated.


  2. October 11th, 2012 at 08:09 | #2

    Hi Marino,

    Thank you for your inquiry. Please let me help you via the official DevExpress Support Channel. Would you please create a new ticket at http://www.devexpress.com/sc?