ReSharper 7.1 "To Property with Backing Field" Moving fields out of place
Asked Answered
M

1

23

I've recently upgraded to R# 7.1 and I'm having this problem where the To Property With Backing Field action displaces my backing fields and moves them to the top of the class.

Example:

Step 1: Define an auto property:

public class MyClass
{
    //... Lots of members here

    public int MyNewProperty {get;set;} // <- Create auto Property
}

Step 2: ReSharper's "To Property With Backing Field"

enter image description here

Expected result:

public class MyClass
{
    //... Lots of members here

    private int _myNewProperty; // <- Backing field immediately above property
    public int MyNewProperty 
    {
       get
       {
           return _myNewProperty;
       }
       set
       {
           _myNewProperty = value;
       }
    }
}

Obtained Result:

public class MyClass
{
    private int _myNewProperty; // <- Backing field on top of the class

    //... Lots of members here


    public int MyNewProperty 
    {
       get
       {
           return _myNewProperty;
       }
       set
       {
           _myNewProperty = value;
       }
    }
}

I've already been playing with Type Members Layout configuration by commenting the "instance fields" part, like this:

<!--instance fields-->
<!--<Entry>
       <Match>
            <And>
               <Kind Is="field"/>
               <Not>
                   <Static/>
               </Not>
            </And>
       </Match>
       <Sort>
           <Readonly/>
           <Name/>
       </Sort>
    </Entry>-->

But I still get the same behavior.

Q: How can I prevent this behavior and revert it to the V6.X one?

Meghanmeghann answered 19/7, 2013 at 21:29 Comment(7)
I don't have ReSharper, so I can't test, but what happens if you remove <Name/> from the <Sort> tag?Anett
@newStackExchangeInstance the whole thing is commented out. I thought that would fix the problem, but it didn'tMeghanmeghann
Try uncommenting it and doing that, see what happens.Anett
@newStackExchangeInstance just tried. Same Behavior =(Meghanmeghann
I feel your pain, at this point though I kind of just adapted and moved on. V8 is available now maybe it will have it as a setting.Smolt
The Type Members Layout won't help. It clearly separates the members by kind, and what you want is the opposite. It would appear that what you ask is not possible with just ReSharper.Rickierickman
Still not possible with resharper alone, likely because backing fields are typically grouped together at the top of a file in .Net.Castiron
S
10

Here is the comment in Russian from JetBrains developer. The article is devoted to R# 8 release. He said that placing private fields together at the beginning is much more common use case than placing it near property. He advised to open ticket in their feedback system. Moreover, he said that maybe they introduce such setting in version 8.1.
In short, it is not possible now.

Stipulate answered 25/7, 2013 at 14:36 Comment(1)
A ticket has been opened for this and is scheduled for V9.1 at the moment: youtrack.jetbrains.com/issue/RSRP-411980Secluded

© 2022 - 2024 — McMap. All rights reserved.