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

3

15

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?

Sassoon answered 9/7, 2010 at 17:2 Comment(0)
H
12

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.

Hardball answered 9/7, 2010 at 17:30 Comment(1)
Nowadays it's called File Layout and is easy to find if you search in Resharper Options. Worth mentioning that the COM interfaces or structs is found on the same level as Default Pattern, if you have already navigated away from Patterns.Scabby
A
10

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.

Aerodynamics answered 9/7, 2010 at 17:8 Comment(1)
@user764754 - It appears to work with structs but not classes in the current version of Resharper. This probably follows the semantics of 'StructLayoutAttribute'Aerodynamics
K
2

Another useful attribute to apply to your fields is [FieldOrder(1)], so you explictly define the order ... I like it as a just-in-case to guard against the fields ever being re-ordered

Not sure what version of FileHelpers this came in with ... I'm using the v2.9.9.0 installed using NuGet

Kilo answered 1/2, 2012 at 10:44 Comment(2)
I ended up creating an entire meta-project for FileHelpers that allows me to stop caring about the order of fields to relate to the order of columns in a csv file. If it ever gets opened sourced I'll follow up here with it.Sassoon
FWIW we eventually abandoned usage of my FileHelper bridge and at this point I use joshclose.github.io/CsvHelper any time i need CSVsSassoon

© 2022 - 2024 — McMap. All rights reserved.