how to group methods by access modifier

Apr 23, 2010 at 8:29 PM
Edited Apr 23, 2010 at 8:32 PM


All --

Please help.

Does anyone know how to group methods by access modifier?

StyleCop has the following default rules...

Error 2 SA1202: All private methods must be placed after all public methods.

Error 5 SA1202: All private methods must be placed after all protected methods.

I tried doing this...

...

<Region Name="MethodsPublic" Type="Region" PaddingBetween="1" PaddingBefore="1" PaddingAfter="1" >
 <Where>
  <Condition Part="EntityType" Operator="Equal" Value="Method" />
  <Condition Part="Name" Operator="Equal" Value="[^_]+" />
  <Condition Part="IsStatic" Operator="Equal" Value="false"/>
  <Condition Part="IsPublic" Operator="Equal" Value="true"/>
  <Condition Part="IsPrivate" Operator="Equal" Value="false"/>
 </Where>
 <OrderBy>
  <Parameter Part="Name"/>
  <Parameter Part="NumberOfParameters" />
 </OrderBy>
</Region>
<Region Name="MethodsPrivate" Type="Region" PaddingBetween="1" PaddingBefore="1" PaddingAfter="1" >
 <Where>
  <Condition Part="EntityType" Operator="Equal" Value="Method" />
  <Condition Part="Name" Operator="Equal" Value="[^_]+" />
  <Condition Part="IsStatic" Operator="Equal" Value="false"/>
  <Condition Part="IsPublic" Operator="Equal" Value="false"/>
  <Condition Part="IsPrivate" Operator="Equal" Value="true"/>
 </Where>
 <OrderBy>
  <Parameter Part="Name"/>
  <Parameter Part="NumberOfParameters" />
 </OrderBy>
</Region>

...

...but, after looking at the SharpSort XSD, it looks like SharpSort does not have Conditions for IsPublic or IsPrivate.

Is there a workaround or is this a feature request?

Please advise.

(For now, I am going to turn off that StyleCop rule; but, I would really like to turn it back on someday as StyleCop is our gold-standard and we deprecate turning off any default StyleCop rules in our shop.)

What do you think?

Thank you.

-- Mark Kamoski

Coordinator
May 26, 2010 at 11:55 AM

You have this possibility of ordering according to access modifier.

<Region Name="Classes" Type="Region" PaddingBetween="1">  

 <Where>    

 <Condition Part="EntityType" Operator="Equal" Value="Class" />    

</Where>  

 <OrderBy> 

   <Parameter Part="AccessModifier" Direction="Ascending"/>    </OrderBy>  </Region>

Dec 6, 2010 at 9:44 AM

Unfortunately that only allows alphabetic sorting:-

internal, private, protected, public

This still violates StyleCop default rules. It would need to be in the order defined by StyleCop:-

public, internal, protected, private

For this reason, I am unable to use the add-in currently. Our team uses default StyleCop rules very strictly.

 

Incidentally, the alphabetic sorting doesn't seem to work correctly. It gave me the order:-

internal, protected, private, public

Coordinator
Dec 17, 2010 at 8:45 PM

<Where>    

 <Condition Part="AccessModifier" Operator="Equal" Value="public" />

</Where>

 <OrderBy>....</OrderBy>

<Where>    

 <Condition Part="AccessModifier" Operator="Equal" Value="private" />

</Where>

 <OrderBy>....</OrderBy>

<Where>    

 <Condition Part="AccessModifier" Operator="Equal" Value="protected" />

</Where>

 <OrderBy>....</OrderBy>

<Where>    

 Or something like that... Figure it out...