Encapsulating Fields: Prevent ReSharper from changing direct access within class to access over accessor

Hi,

ReSharper helps one to encapsulate fields in c#. However, everytime I am encapsulating a field, resharper changes the direct access on a field within the same class to access over the newly created accessor. This results in somtehing like this:


before encapsulating:

private string name;

public void foo(){

   name = "bar";
}




after encapsulting:

private string name;

public string Name {
  get{ return name; }
  set { name = value;}

}

public void foo(){

   Name = "bar"; //here is the problem. It shouldn't use the accessor but the field directly
}


I searched in the settings to prevent this behaviour, but didn't find anything. In the encapsulate field window is a checkbox "Replace external usages only" but it is grayed out (like on this screenshot from the resharper manual: https://www.jetbrains.com/resharper/webhelp/Refactorings__Encapsulate_Field.html). Is there any way to prevent the conversion from direct access to access over the accessor within the same class? Due to this, it is actually  more comfortable to write the accessor by one self than let resharper create them and changing the access over the accessor back to direct access.

2 comments
Comment actions Permalink

It sounds like you want the Generate Properties option, rather than the Encapsulate Field option.

0
Comment actions Permalink

Yes, that is exactly what I want. I have searched the entire options menu, but never had a look on the "generate code" feature...

0

Please sign in to leave a comment.