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?

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258

3 Answers3

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.

SteveC
  • 15,808
  • 23
  • 102
  • 173
Romain Verdier
  • 12,833
  • 7
  • 57
  • 77
  • 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_. – wezzix Jan 24 '17 at 10:55
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.

Will Dean
  • 39,055
  • 11
  • 90
  • 118
  • @user764754 - It appears to work with structs but not classes in the current version of Resharper. This probably follows the semantics of 'StructLayoutAttribute' – Will Dean Feb 01 '16 at 13:16
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

SteveC
  • 15,808
  • 23
  • 102
  • 173
  • 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. – Chris Marisic Feb 01 '12 at 17:04
  • 1
    FWIW we eventually abandoned usage of my FileHelper bridge and at this point I use http://joshclose.github.io/CsvHelper/ any time i need CSVs – Chris Marisic Feb 16 '16 at 19:59