C# 如何使用 Resharper 重新排序类型成员?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1440131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:34:14  来源:igfitidea点击:

How to reorder type members with Resharper?

c#resharper

提问by AngryHacker

Typical scenario: a class that a lot of people have worked on. I'd like to sort methods, properties, etc... in alphabetical order.

典型场景:一个很多人都做过的课程。我想按字母顺序对方法、属性等进行排序。

I'd like to be able to do this within the region or globally in the class.

我希望能够在该地区或全球范围内在课堂上做到这一点。

I see the feature in Resharper to do it, but it does not seem to do anything.

我在 Resharper 中看到了这样做的功能,但它似乎没有做任何事情。

采纳答案by Lucero

Use the "Cleanup Code" functionality.

使用“清理代码”功能。

The order of the members can be set up in the ReSharper options in Languages, C#, Type Members Layout. This is a well documented XML layout specification which ReSharper uses when reordering members.

成员的顺序可以在 Languages、C#、Type Members Layout 的 ReSharper 选项中设置。这是 ReSharper 在重新排序成员时使用的一个有据可查的 XML 布局规范。

回答by TrueWill

An alternative to consider is Regionerate. We use and like ReSharper, but Regionerate fits our needs for creating regions and sorting/rearranging members. And it's all customizable, of course.

另一种考虑是Regionerate。我们使用并喜欢 ReSharper,但 Regionerate 适合我们创建区域和排序/重新排列成员的需求。当然,这一切都是可定制的。

UPDATE: We've started using ReSharper's Code Cleanupfor this instead.

更新:我们已经开始为此使用ReSharper 的代码清理

回答by Clint StLaurent

Two things: There is a known (but not heavily documented) condition where pre-compile conditionals (#if DEBUG for example) will stop type member reordering. http://youtrack.jetbrains.com/issue/RSRP-336643#tab=CommentsIn other words if you have #IF DEBUG then it won't reorder.

两件事:有一个已知(但没有大量记录)的条件,其中预编译条件(例如#if DEBUG)将停止类型成员重新排序。 http://youtrack.jetbrains.com/issue/RSRP-336643#tab=Comments换句话说,如果你有 #IF DEBUG 那么它就不会重新排序。

I also recently noticed that in ReSharper 8.0.1 (and probably earlier versions) that the button to revert the XML template back to DEFAULT WITH REGIONS doesn't really have any statements to include #REGION grouping. So I took a StyleCop friendly template that includes sorting and added #REGION-ing to each type member. If you select CUSTOM TEMPLATE then paste in this XML it should work.

我最近还注意到,在 ReSharper 8.0.1(可能还有更早的版本)中,将 XML 模板恢复为 DEFAULT WITH REGIONS 的按钮实际上没有任何包含 #REGION 分组的语句。因此,我采用了 StyleCop 友好模板,其中包括对每个类型成员进行排序和添加 #REGION-ing。如果您选择 CUSTOM TEMPLATE 然后粘贴这个 XML,它应该可以工作。

<Patterns xmlns="urn:shemas-jetbrains-com:member-reordering-patterns">

<!--  Do not reorder COM interfaces  -->
<Pattern>
    <Match>
        <And Weight="100">
            <Kind Is="interface" />
            <HasAttribute CLRName="System.Runtime.InteropServices.InterfaceTypeAttribute" />
        </And>
    </Match>
</Pattern>

<!--  Special formatting of NUnit test fixture  -->
<Pattern RemoveAllRegions="true">
    <Match>
        <And Weight="100">
            <Kind Is="class" />
            <HasAttribute CLRName="NUnit.Framework.TestFixtureAttribute" Inherit="true" />
        </And>
    </Match>

    <!--  Setup/Teardow  -->
    <Entry>
        <Match>
            <And>
                <Kind Is="method" />
                <Or>
                    <HasAttribute CLRName="NUnit.Framework.SetUpAttribute" Inherit="true" />
                    <HasAttribute CLRName="NUnit.Framework.TearDownAttribute" Inherit="true" />
                    <HasAttribute CLRName="NUnit.Framework.FixtureSetUpAttribute" Inherit="true" />
                    <HasAttribute CLRName="NUnit.Framework.FixtureTearDownAttribute" Inherit="true" />
                </Or>
            </And>
        </Match>
    </Entry>
    <!--  All other members  -->
    <Entry />
    <!--  Test methods  -->
    <Entry>
        <Match>
            <And Weight="100">
                <Kind Is="method" />
                <HasAttribute CLRName="NUnit.Framework.TestAttribute" Inherit="false" />
            </And>
        </Match>
        <Sort>
            <Name />
        </Sort>
    </Entry>
</Pattern>

<!--  Default pattern  -->


<Pattern RemoveAllRegions="false">
    <!--  Delegates  -->
    <Entry>
        <Match>
            <And Weight="100">
                <Access Is="public" />
                <Kind Is="delegate" />
            </And>
        </Match>
        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Name />
        </Sort>
        <Group Region="Delegates" />
    </Entry>


    <!--  Fields and constants  -->
    <Entry>
        <Match>
            <Or>
                <Kind Is="field" />
                <Kind Is="constant" />
            </Or>
        </Match>

        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Kind Order="constant" />
            <Readonly />
            <Static />
            <Name />
        </Sort>
        <Group Region="Fields" />
    </Entry>

    <!--  Enums  -->
    <Entry>
        <Match>
            <Kind Is="enum" />
        </Match>
        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Name />
        </Sort>
        <Group Region="Enums" />
    </Entry>

    <!--  Constructors. Place static one first  -->
    <Entry>
        <Match>
            <Kind Is="constructor" />
        </Match>
        <Sort>
            <Static />
            <Access Order="public internal protected-internal protected private" />
        </Sort>
        <Group Region="Constructors" />
    </Entry>

    <!--  Destructors. Place static one first  -->
    <Entry>
        <Match>
            <Kind Is="destructor" />
        </Match>
        <Sort>
            <Static />
            <Access Order="public internal protected-internal protected private" />
        </Sort>
        <Group Region="Destructors" />
    </Entry>


    <!--  Events  -->
    <Entry>
        <Match>
            <Kind Is="event" />
        </Match>

        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Name />
        </Sort>
        <Group Region="Events" />
    </Entry>

    <!--  Properties  -->
    <Entry>
        <Match>
            <And>
                <Kind Is="property" />
                <Not>
                    <Kind Is="indexer" />
                </Not>
            </And>
        </Match>
        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Static />
            <Abstract />
            <Virtual />
            <Override />
            <Name />
        </Sort>
        <Group Region="Properties" />
    </Entry>

    <!--  Indexers  -->
    <Entry>
        <Match>
            <Kind Is="indexer" />
        </Match>
        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Static />
            <Abstract />
            <Virtual />
            <Override />
            <Name />
        </Sort>
        <Group Region="Indexers" />
    </Entry>

    <!--  Methods  -->
    <Entry>
        <Match>
            <And>
                <Or>
                    <Kind Is="method" />
                    <Kind Is="operator" />
                    <HandlesEvent />
                </Or>
                <Not>
                    <Kind Is="destructor" />
                </Not>
            </And>
        </Match>
        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Static />
            <Abstract />
            <Virtual />
            <Override />
            <Name />
        </Sort>
        <Group Region="Methods" />
    </Entry>

    <!--  all other members  -->
    <Entry />

    <!--  nested types  -->
    <Entry>
        <Match>
            <Kind Is="type" />
        </Match>
        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Static />
            <Abstract />
            <Virtual />
            <Override />
            <Name />
        </Sort>
        <Group Region="Nested Types" />
    </Entry>
</Pattern>

回答by Ian Griffiths

For the benefit of people, like me, who landed on this question through a web search but found that the detail of the question wasn't quite what they were expecting, you might like to know that you can move individual members up and down within the file by holding down Ctrl-Alt-Shift and then pressing the up or down arrows.

为了像我这样通过网络搜索找到这个问题但发现问题的细节并不完全符合他们预期的人的利益,您可能想知道您可以在内部上下移动单个成员按住 Ctrl-Alt-Shift,然后按向上或向下箭头来打开文件。

(Obviously that's not the automated arrangement by alphabetical order being asked for in the body of the question, but it was the answer I was hoping I would find for the question in the title.)

(显然,这不是问题正文中要求的按字母顺序自动排列,但这是我希望能在标题中找到问题的答案。)

回答by Do?an Etkin

From Visual Studio menu;

从 Visual Studio 菜单;

ReSharper > Options > Environment > IntelliSense > Completion Behaviour > Sort Items(Alphabetically)

ReSharper > 选项 > 环境 > IntelliSense > 完成行为 > 排序项目(按字母顺序)

回答by jgauffin

Sorting is not activated by default. You can activate it by opening the resharper options and then go here:

默认情况下不激活排序。您可以通过打开 resharper 选项来激活它,然后转到这里:

enter image description here

在此处输入图片说明

回答by birdamongmen

If you are reordering parameters on specific methods, you can use the Refactor > Change Signature if your cursor is on a method name. I use the IntelliJ shortcuts, so for me, the command is Ctrl+Shift+R followed by Ctrl+F6.

如果您要对特定方法的参数重新排序,并且光标位于方法名称上,则可以使用重构 > 更改签名。我使用 IntelliJ 快捷方式,所以对我来说,命令是 Ctrl+Shift+R,然后是 Ctrl+F6。

Refactor context menu

重构上下文菜单

After doing so, a dialog will pop up which allows you the reorder method parameters. It will even refactor any implementations of an interface.

执行此操作后,将弹出一个对话框,允许您重新排序方法参数。它甚至会重构接口的任何实现。

回答by Robin Bennett

jgauffin's answer is close, but I found that (with R# 2017) to reorder Properties I needed to click the 'XAML' option in the header of the File Layout dialog and change

jgauffin 的答案很接近,但我发现(使用 R# 2017)要重新排序属性,我需要单击“文件布局”对话框标题中的“XAML”选项并进行更改

<Entry DisplayName="Properties, Indexers">
  <Entry.Match>
    <Or>
      <Kind Is="Property" />
      <Kind Is="Indexer" />
    </Or>
  </Entry.Match>
</Entry>

to

<Entry DisplayName="Properties, Indexers">
  <Entry.Match>
    <Or>
      <Kind Is="Property" />
      <Kind Is="Indexer" />
    </Or>
  </Entry.Match>
  <Entry.SortBy>
    <Name />
  </Entry.SortBy>
</Entry>

The 'Sort By' property was empty and read-only, which makes sense because it's only used for items with the same name (and all properties should be uniquely named)

'Sort By' 属性为空且只读,这是有道理的,因为它仅用于具有相同名称的项目(并且所有属性都应具有唯一名称)