Formatting Resharper backing fields for properties in C#

北战南征 提交于 2019-12-05 01:33:19

Version 9.1 has a new option:

Resharper > Options > Code Editing > Place backing field above property

https://youtrack.jetbrains.com/issue/RSRP-411980#comment=27-961304

In Resharper 6.1 I have this kind of a template, that may solve the question, or at least help others.

Using the 'shortuct' + TAB combination, I call mine nprop, and got further in adding a comment section.

private $TYPE$ _$NAMEP$;

/// <summary>
/// The $CLASS$ $NAMEC$
/// </summary>
public $TYPE$ $NAME$
{
    get { return _$NAMEP$; }
    set { _$NAMEP$ = value; }
}

Here's an image of the extra customisation to help rename things so it's just a matter of typing 2 values.

Sure, at least with Resharper 5.1.1727 you can add an entry like the following

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

to the Type Members Layout to indicate where you want the backing fields to appear in the class.

For example, if you want fields at the bottom of the class insert that section as the very last entry in the Default Pattern section:

  <!--Default pattern-->
  <Pattern>

Resharper 5.1 comes with a default entry that includes fields:

<!--fields and constants-->
<Entry>
  <Match>
    <Or>
      <Kind Is="constant"/>
      <Kind Is="field"/>
      <Kind Is="event"/>
    </Or>
  </Match>
  <Sort>
    <Kind Order="constant field"/>
    <Static/>
    <Readonly/>
    <Name/>
  </Sort>
</Entry>

so that it will not conflict with your new rule, remove field from the default entry e.g.

<!--events and constants-->
<Entry>
  <Match>
    <Or>
      <Kind Is="constant"/>
      <Kind Is="event"/>
    </Or>
  </Match>
  <Sort>
    <Kind Order="constant event"/>
    <Static/>
    <Readonly/>
    <Name/>
  </Sort>
</Entry>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!