Resharper: how to force introducing new private fields at the bottom of the class?

扶醉桌前 提交于 2020-01-14 13:49:07

问题


Resharper offers a very useful introduce and initialize field xxx action when you specify a new parameter in a constructor like:

Constructor (int parameter)

The only (minor) nuisance is that it puts the new field at the beginning of the class - and I'm a fan of putting private parts as far away as possible from the prying eyes of strangers ;).

If, however, you already have some private fields in the class, Resharper will put the new field "correctly" (note the quotes, I don't want to start a flame war over this issue) next to those, even if they are at the end of the class.

Is there a way to force Resharper to always put new fields at the end of the class?

UPDATE: OK, I forgot to mention I know about the "Type Members Layout in Options" feature, but some concrete help on how to modify the template to achieve fields placement would be nice.


回答1:


If you haven't already, you should lodge a bug report at the ReSharper Jira site:

http://www.jetbrains.net/jira/browse/RSRP

Have a look first to see if someone has already reported this. If so, you can vote on their issue and be notified of any changes to it.




回答2:


In the "Type Members Layout" you can find and move the following block of XML

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

To the bottom of the default pattern. Basically two lines up form the bottom of the file

    <!--HERE-->

  </Pattern>      
</Patterns>

Unfortunately a quick test shows that this doesn't affect new fields created through the option you describe, but it will make the code cleanup move them to the right place. (Provided you have "Reorder type members" turned on)


If you are in the habit of using public fields and you only want the private ones moved to the bottom then change the match above to this:

<Match>
  <And>
    <Access Is="private"/>
    <Or>
      <Kind Is="constant"/>
      <Kind Is="field"/>
    </Or>
  </And>
</Match>

Then you can copy the whole block again, change the "Access Is" value to "public" and put the new public block wherever you want the public fields to go - near the top, where it came from originally I'd guess.




回答3:


Use a custom "Type Members Layout" in Resharper options.

In the "Default Pattern" section of the XML layout definition, just move the entry element that groups your fields to the end of the section, after methods, events, properties, etc.



来源:https://stackoverflow.com/questions/858652/resharper-how-to-force-introducing-new-private-fields-at-the-bottom-of-the-clas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!