ReSharper element order and [DataMember] attributes

I would like to define standard WCF data contracts using attributes [DataMember]

. I also want to specify a property Order

. When doing auto-formatting, ReSharper re-orders my properties so that they are not in the order specified in the attribute [DataMember]

.

Now it doesn't affect the compiled code, but it makes it annoying.

Is there a way to make the layout of a ReSharper type member respect the ordering I define in the attributes?

public class MyDataContract
{
    [DataMember(Order = 1)]
    public int One { get; set; }
    [DataMember(Order = 2)]
    public int Two { get; set; }
    [DataMember(Order = 3)]
    public int Three { get; set; }
}

      

Now that One, Two, Three is not in alphabetical order, the default sort is one, three, two. Is there a way to change this? I know that the layout type of the element can see the attributes, but could not find a way to collect data from the attribute and use it when sorting.

Thank!

+3


source to share


1 answer


As far as I can see, ReSharper does not reorder properties by default, so the example you give is not reordering. However, it sorts the fields by name, which may be what you are running into.

Fortunately, you can edit the order using the Options dialog. But as far as I can see you need to specify the class attribute to comply with the applicable rules. In this case, your class can have an attribute [DataContract]

, which makes it nice and lightweight.

ReSharper 9 has a nice shiny new visual editor for defining layout and ordering: Code Editing -> C # -> File Layout. You can take a look at the COM interop rules to see how they are handled to ignore the ordering for COM structs (mostly conform to COM attributes and have no rules).



ReSharper 8 lets you edit the dreaded XML file in a slightly different options page: Code Editing -> C # -> Item Type. Select "Custom Layout" and scroll down until you find the section with the comment "Don't reorder COM interfaces ...". You want to create a new element Pattern

that looks something like this:

<Pattern>
  <Match>
    <And>
      <Kind Is="class"/>
      <HasAttribute CLRName="System.Runtime.Serialization.DataContractAttribute"/>
    </And>
  </Match>
</Pattern>

      

This template will match the class with the attribute [DataContract]

, but it does not specify any rules, so it will not change the order of properties or fields.

+2


source







All Articles