Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a resharper comment directive to disable code cleanup for a class?

Tags:

c#

resharper

I have a class where FileHelpers is dependent on the field order in this class file. If the class file ever gets a code clean up run against it that will cause the fields to be sorted alphabetically and invisibly ruin my class.

Since I would like to avoid this from ever accidentally occuring, is there a resharper comment directive to disable code cleanup for a class?

like image 731
Chris Marisic Avatar asked Jul 09 '10 17:07

Chris Marisic


People also ask

How do I run ReSharper cleanup?

Select ReSharper | Options from the main menu or press Alt+R O . Go to the cleanup profiles settings page: Code Editing | Code Cleanup | Profiles. Select which Code Cleanup profile should be applied on save and click Set as default (the default profile is also used for silent cleanup).

How do you code cleanup?

Right-click on the project or solution name in Solution Explorer, select Analyze and Code Cleanup, and then select Run Code Cleanup.

Which event procedure provides a clean up code?

Code cleanup refers to the act of writing code so that it cleans up leftover data structures and other unwanted materials from memory and the filesystem. It is sometimes treated as a synonym of refactoring code, which involves making the source code itself easier to understand, maintain, and modify.


2 Answers

You can customize the default member layout XML file and specify a pattern you want to ignore during the "reorder members" step of a code cleanup.

Have a look at the Type Member Layout section under the Resharper settings. You can see that there already are two exceptions defined for COM interfaces and Structs with the StructLayoutAttribute:

 <!--Do not reorder COM interfaces-->
  <Pattern>
    <Match>
      <And Weight="100">
        <Kind Is="interface"/>
        <HasAttribute 
           CLRName="System.Runtime.InteropServices.InterfaceTypeAttribute"/>
      </And>
    </Match>
  </Pattern>

<!--Do not reorder when StructLayoutAttribute is set -->
  <Pattern>
    <Match>
      <And Weight="100">
     <Or>
        <Kind Is="struct"/>
        <Kind Is="class"/>
     </Or>
        <HasAttribute 
           CLRName="System.Runtime.InteropServices.StructLayoutAttribute"/>
      </And>
    </Match>
  </Pattern>

You could easily create your own IgnoreTypeMemberReorderingAttribute and add a small section in the XML file that check against it.

like image 104
Romain Verdier Avatar answered Sep 27 '22 17:09

Romain Verdier


I believe Resharper observes the [StructLayout(LayoutKind.Sequential)] attribute.

Update: I think this worked for classes at the time of writing, but in current versions of Resharper (10), it appears that it only applies to structs, not classes. So it's probably still useful in lots of interop situations, but is not a general way of holding onto the order of any class.

like image 39
Will Dean Avatar answered Sep 27 '22 17:09

Will Dean