开发者

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

开发者 https://www.devze.com 2023-01-06 03:33 出处:网络
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 a

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开发者_StackOverflow社区 accidentally occuring, is there a resharper comment directive to disable code cleanup for a class?


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消