C# 切换枚举自动填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19778108/
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-10 15:51:55  来源:igfitidea点击:

Switch enum auto-fill

c#visual-studio-2013

提问by Metaphor

I was typing a switch with an enum in VS 2013 and all case statements filled out automatically after I finished the switch. Now I can't repeat it. I was not hallucinating, the switch filled out with all enum options, one per case, automatically. Looked through MS docs for VS 2013 and didn't find it.

我在 VS 2013 中输入了一个带有枚举的 switch,并且在我完成 switch 后自动填写了所有 case 语句。现在我不能重复了。我没有出现幻觉,开关自动填写了所有枚举选项,每个案例一个。浏览了 VS 2013 的 MS 文档,但没有找到。

I use quite a few enums and this feature will save me a ton of time if I can find what it is I did to trigger it. Can anyone help?

我使用了很多枚举,如果我能找到触发它的操作,此功能将为我节省大量时间。任何人都可以帮忙吗?

采纳答案by Marvin Smit

Use the code snipped sw(tab)(tab)

使用代码剪断 sw(tab)(tab)

Hope this helps,

希望这可以帮助,

回答by GSerjo

By default, Visual Studion's snippet works correct. You should type "sw" and then press double "Tab".

默认情况下,Visual Studion 的代码段工作正常。您应该输入“sw”,然后按双“Tab”。

If you use Resharper, the snippet doesn't work, because Resharper's snippet has more priority, by default. So, you should turn off resharper's snippet.

如果您使用 Resharper,则该代码段不起作用,因为默认情况下,Resharper 的代码段具有更高的优先级。因此,您应该关闭 resharper 的代码段。

Go to "Resharper" -> "Template Explorer"-> "C#" then uncheck "switch". Try "sw" + double "Tab"

转到“Resharper”->“模板资源管理器”->“C#”,然后取消选中“开关”。尝试“sw”+双“Tab”

回答by jmsb

Notice:This answer applies to performing the switch/enum autogeneration while also using Resharper.

注意:此答案适用于在使用 Resharper 的同时执行切换/枚举自动生成。

Using Visual Studio 2013 and Resharper 8.2, the previously mentioned methods do not work. Here's how to actually get this generation to work when using Resharper. Hopefully it will save someone the fifteen minutes I just spent figuring this out.

使用 Visual Studio 2013 和 Resharper 8.2,前面提到的方法不起作用。以下是在使用 Resharper 时如何实际让这一代工作。希望它可以节省我刚刚花在解决这个问题上的十五分钟。

Performing "sw(tab)(tab)" will only generate the following:

执行 "sw(tab)(tab)" 只会生成以下内容:

switch (nameOfEnumVariable)
{

}

Resharper can generate the labels using Alt + Enter(if your cursor stands in the switch statement) and selecting Generate switch labelsas in the following screenshot:

Resharper 可以使用Alt + Enter生成标签(如果您的光标位于 switch 语句中)并选择生成开关标签,如下面的屏幕截图所示:



Using the Resharper menu to fill in a switch statement

使用 Resharper 菜单填写 switch 语句



The result looks like this:

结果如下所示:



enter image description here

在此处输入图片说明



回答by 00jt

The selected answer is mostly correct, you don't need Resharper as other's have suggested (at least not with Visual Studio Professional 2012+).

所选答案大部分是正确的,您不需要像其他人建议的那样使用 Resharper(至少在 Visual Studio Professional 2012+ 中不需要)。

1) type "sw" then "[tab][tab]" (as Marvin Smitsaid)

1)输入“sw”然后输入“[tab][tab]”(Marvin Smit如上所述)

Which (as jmblacksaid) will generate something like:

哪个(jmblack如上所述)会产生类似的东西:

  switch (switch_on)
  {
            default:
  }

but then

但是之后

2) you need to select which thing to enumerate on (switch_onwill be highlighted still at this point). So type in the Enum (or your variable of the enum type) while switch_onis highlited and hit [Enter][Enter].

2)您需要选择要枚举的内容(此时switch_on仍将突出显示)。因此,在高亮显示的同时switch_on输入Enum(或您的枚举类型变量),然后按 [Enter][Enter]。

(I just confirmed this worked on my machine running VS2012, and i'm fairly certain this is the same thing i have done on my other machine running VS2013, and i haven't tested other versions of VS (ultimate/express/etc.))

(我刚刚确认这在我运行 VS2012 的机器上有效,我相当确定这与我在另一台运行 VS2013 的机器上所做的相同,而且我还没有测试过其他版本的 VS(ultimate/express/etc. ))

回答by chviLadislav

Visual studio 2017, 2019 - without Resharper:

1) write "switch"
2) press two times TAB, then you will see:

Visual Studio 2017、2019 - 没有 Resharper:

1)写“开关”
2)按两次 TAB,然后你会看到:

switch (switch_on)
{
        default:
}

(the switch_on is highlited)
3) retype switch_onto your enum variable or type
4) press ENTER or click somewhere else (pressing TAB does not work), now you should see all the enum items filled:

(switch_on 被高亮显示)
3)重新输入switch_on您的枚举变量或输入
4)按 ENTER 或单击其他地方(按 TAB 不起作用),现在您应该看到所有的枚举项已填充:

switch (YOUR_ENUM_VARIABLE_OR_TYPE)
{
    case YOUR_ENUM_TYPE.Item1:
        break;
    case YOUR_ENUM_TYPE.Item2:
        break;
    case YOUR_ENUM_TYPE.Item3:
        break;
    default:
        break;
}

回答by Simon Mourier

I've written a free and open source extension, based on Roslyn, for Visual Studio 2015 and 2017, that not only allows to fill the switch case for an enum, but is also capable of adding new cases if enum values (fields) have been added to the enum type definition, or sort the list of cases by value or name.

我已经为 Visual Studio 2015 和 2017编写了一个基于Roslyn的免费开源扩展,它不仅允许填充枚举的 switch case,而且如果枚举值(字段)具有,还能够添加新的 case已添加到枚举类型定义中,或按值或名称对案例列表进行排序。

It's available here: Enum Case Generator

它在此处可用:Enum Case Generator

This is how to use it:

这是如何使用它:

enter image description here

在此处输入图片说明

回答by marcelom

I think what you need is this:

我想你需要的是这个:

sw(tab)(tab)enumVariableName(tab)(downArrow)

I tested it and works ( in VS2013 at least).

我测试了它并且可以工作(至少在 VS2013 中)。

回答by Cyril Delander

Hi I just ran into the same problem, I just found out that when you do:

嗨,我刚刚遇到了同样的问题,我刚刚发现当你这样做时:

switch(nameofvariable){
  default:
  break;
}

when you are filling the variable and you double click on the variable of your choice (the enum) it will give you all the cases

当您填充变量并双击您选择的变量(枚举)时,它将为您提供所有情况

回答by Ali Karaca

VS 2019 + resharper create an empty switch with your variable. Click on first curly bracket "{" Then press alt + enterYou will see Generate switch labels. Screenshot below:

VS 2019 + resharper 使用您的变量创建一个空开关。单击第一个大括号“ {”然后按alt + enter您将看到生成开关标签。截图如下:

Generate switch labels

生成开关标签