-->

Formatting Resharper backing fields for properties

2020-08-17 06:27发布

问题:

So this is a follow-up to Keith Nicholas' question from 2 years ago:

Formatting Resharper backing fields for properties in C#

My guess is this would probably involve a custom Type Members Layout. Is there a way to to this yet in the latest Resharper version (as of now, it's 5.1) yet?

回答1:

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



回答2:

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.



回答3:

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>


回答4:

As of today: Still Not Possible!
(see http://hadihariri.com/2011/01/04/in-depth-look-at-customizing-type-layout-with-resharper/#comment-5738)



标签: c# resharper