4

I'm using this NetResource class to send files to a network drive and it looks like this:

[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
    public ResourceScope Scope;
    public ResourceType ResourceType;
    public ResourceDisplayType DisplayType;
    public int Usage;
    public string LocalName;
    public string RemoteName;
    public string Comment;
    public string Provider;
}

Now it's very important that the order of these fields stay the same, as hinted on by the StructLayout attribute.

However, when someone would run a resharper cleanup, resharper decides to move the fields around and that would break the code.

Is there any way of telling rehsarper to not mess with it? I feel like if I can't do that, someone is going to eventually break the code and have no idea where to look.

But a mediocre solution to that I think would be to create a unittest that can check if there layout is as expected.

Edit: I've seen this answer, but it is outdated and requires resharper settings to be updated. I will also not be guaranteed that coworkers use this resharper setting. I'm looking for a way to add it in the code, just like you can do // ReSharper disable once InconsistentNaming

Tvde1
  • 1,246
  • 3
  • 21
  • 41
  • Possible duplicate of [Is there a resharper comment directive to disable code cleanup for a class?](https://stackoverflow.com/questions/3214894/is-there-a-resharper-comment-directive-to-disable-code-cleanup-for-a-class) – mxmissile Oct 30 '18 at 13:42

1 Answers1

3

I see a couple of solutions here:

  • You might mark the class with NoReorderAttribute from the JetBrains.Annotations (there are several ways to add them to a project). Then ReSharper will stop reordering members inside the marked code entity.
  • It is mostly about already mentioned answer, I will show you how to get the same things in last ReSharper builds. All you need is to add "System.Runtime.InteropServices.StructLayoutAttribute" to "Non-reorderable types" pattern in ReSharper | Options | Code Editing | C# | File Layout.

Step 1: enter image description here

Step 2:enter image description here

Step 3: enter image description here

Step 4:enter image description here

Step 5:enter image description here

To make sure your colleagues use the same settings in ReSharper, save this change to the Solution team shared layer (Save To at the bottom of the Options dialog). Then if any of your colleagues opens the solution, ReSharper will automatically use the setting from this layer with no additional actions required.

Alexander Kurakin
  • 13,373
  • 2
  • 33
  • 29