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?
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).
Right-click on the project or solution name in Solution Explorer, select Analyze and Code Cleanup, and then select Run Code Cleanup.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With