How to refactor this huge switch statement?

I've inherited some code and within it is a 500 line switch statement. Basically, it switches on a string task and executes the corresponding actions. I have since moved each case statement in to their own method in a new class. The giant switch statement still exists but instead of inlining the logic each case just calls a method so it's much neater. The problem is that the methods

如何重构这个巨大的switch语句?

我已经继承了一些代码,并在其中是一个500行转换语句。 基本上,它打开一个string任务并执行相应的操作。 我已经将每个案例陈述在新课堂中转移到他们自己的方法中。 巨大的switch语句仍然存在,但不是内联逻辑,而是每种情况都只是调用一种方法,所以它更简洁。 问题是这些方法修改了很多不同的东西。 50%的方法需要传入0个参数。大约40%需要5个参数,剩下的10%需要10个参数。 目前这有效,但我想让它变得更好。 要

How should I convert a string to an enum in C#?

What's the best way to convert a string to an enumeration value in C#? I have an HTML select tag containing the values of an enumeration. When the page is posted, I want to pick up the value (which will be in the form of a string) and convert it to the enumeration value. In an ideal world, I could do something like this: StatusEnum MyStatus = StatusEnum.Parse("Active"); but that isn

我应该如何将字符串转换为C#中的枚举?

将字符串转换为C#中的枚举值的最佳方式是什么? 我有一个包含枚举值的HTML选择标记。 当页面发布时,我想获取值(它将以字符串的形式)并将其转换为枚举值。 在理想的世界中,我可以做这样的事情: StatusEnum MyStatus = StatusEnum.Parse("Active"); 但这不是有效的代码。 在.NET Core和.NET> 4中有一个通用的分析方法: Enum.TryParse("Active", out StatusEnum myStatus); 这也包括C#7的新型直列out变量,所

How to get an IEnumerable<string> of enum values attributes?

I have a StringValue attribute for enums values, so I could attach a description to each value: public class StringValueAttribute : Attribute { public string Value { get; private set; } public StringValueAttribute(string value) { Value = value; } } And that is how I use it: enum Group { [StringValue("Computer Science")] ComputerScience, [StringValue("Softw

如何获得枚举值属性的IEnumerable <string>?

我有一个枚举值的StringValue属性,所以我可以附加一个描述到每个值: public class StringValueAttribute : Attribute { public string Value { get; private set; } public StringValueAttribute(string value) { Value = value; } } 这就是我使用它的方式: enum Group { [StringValue("Computer Science")] ComputerScience, [StringValue("Software Engineering")] SoftwareEn

Can you access a long description for a specific enum value

I usually access enum description for a corresponding value like: Enum.GetName(typeof(MyEnum), myid); I need to have an enum that could use any chars like "hello world %^$£%&" I've seen people attaching an attribute and adding extensions like here: http://weblogs.asp.net/stefansedich/archive/2008/03/12/enum-with-string-values-in-c.aspx but I can't work out if this

你能访问一个特定枚举值的长描述吗?

我通常访问enum描述为相应的值,如: Enum.GetName(typeof(MyEnum),myid); 我需要一个枚举,可以使用任何字符,如“你好世界%^ $£%&” 我看到有人附加属性并添加如下扩展名: http://weblogs.asp.net/stefansedich/archive/2008/03/12/enum-with-string-values-in-c.aspx 但我无法解决这个问题是否可以用来访问长描述。 任何人做过类似的事情 谢谢 戴维 为什么它不能解决? 您可以通过从属性中插入来

C# vs Java Enum (for those new to C#)

I've been programming in Java for a while and just got thrown onto a project that's written entirely in C#. I'm trying to come up to speed in C#, and noticed enums used in several places in my new project, but at first glance, C#'s enums seem to be more simplistic than the Java 1.5+ implementation. Can anyone enumerate the differences between C# and Java enums, and how to overco

C#vs Java Enum(适用于C#的新手)

我一直在Java中编程一段时间,刚刚被抛到一个完全用C#编写的项目上。 我试图在C#中提高速度,并注意到在我的新项目中有几个地方使用了枚举,但乍一看,C#的枚举似乎比Java 1.5+实现更简单。 任何人都可以枚举C#和Java枚举之间的区别,以及如何克服差异? (我不想开始一场语言的火焰战,我只想知道如何在C#中做一些我曾经在Java中做过的事情)。 例如,有人可以发布与Sun着名的Planet枚举示例相对应的C#对象吗? publi

c# loop through all fields of enum assigning values from string array

I am building a Soap Body for a web-service, and there are dozens of optional fields. Currently I have been handling these like this: wsSoapBody.OrderType = aMessage[(int)cardCreate.OrderType].ToString(); wsSoapBody.ActivateFlag = Convert.ToInt32(aMessage[(int)cardCreate.ActivateFlag].ToString()); //P-02925; if (aMessage[(int)cardCreate.ShipDate].ToString() != ""){ wsSoapBody.

c#循环遍历从字符串数组中分配值的枚举字段

我正在为网络服务构建一个Soap Body,并且有几十个可选字段。 目前我一直在处理这样的事情: wsSoapBody.OrderType = aMessage[(int)cardCreate.OrderType].ToString(); wsSoapBody.ActivateFlag = Convert.ToInt32(aMessage[(int)cardCreate.ActivateFlag].ToString()); //P-02925; if (aMessage[(int)cardCreate.ShipDate].ToString() != ""){ wsSoapBody.ShipmentDate = Convert.ToDateTime(aMessage[(int

Use an enum to select which class to instantiate

I have an enum that I am trying to associate to dto's: public enum DtoSelection { dto1, dto2, dto3, } There are 108 and values in this enum. I have a dto object for each of these dto's: public class dto1 : AbstractDto { public int Id { get; set; } //some stuff specific to this dto } I am trying to make a method (eventually a service) that will retu

使用枚举来选择要实例化的类

我有一个枚举,我试图联系到dto的: public enum DtoSelection { dto1, dto2, dto3, } 这个枚举中有108个值。 我有这些dto的每个dto对象: public class dto1 : AbstractDto { public int Id { get; set; } //some stuff specific to this dto } 我正在尝试创建一个方法(最终是一个服务),它将返回一个与该dto关联的类型的新dto对象: private AbstractDto(int id) { if (id

Loop through a C# enum's keys AND values

This question already has an answer here: How do I enumerate an enum? 26 answers Enum类具有您正在寻找的方法。 foreach(int i in Enum.GetValues(typeof(stuff))) { String name = Enum.GetName(typeof(stuff), i); NewObject thing = new NewObject { Name = name, Number = i }; } 你可以使用LINQ(几乎总是): List<NewObject> stuff = Enum.GetValues(typeof(stuffEnum)).

循环访问C#枚举的键和值

这个问题在这里已经有了答案: 我如何列举一个枚举? 26个答案 Enum类具有您正在寻找的方法。 foreach(int i in Enum.GetValues(typeof(stuff))) { String name = Enum.GetName(typeof(stuff), i); NewObject thing = new NewObject { Name = name, Number = i }; } 你可以使用LINQ(几乎总是): List<NewObject> stuff = Enum.GetValues(typeof(stuffEnum)).Cast<stuffEnum>(

Display enum in ComboBox with spaces

I have an enum, example: enum MyEnum { My_Value_1, My_Value_2 } With : comboBox1.DataSource = Enum.GetValues(typeof(MyEnum)); But now my question: How can I replace the "_" with " " so that it becomes items with spaces instead of underscores? And that a databound object still works If you have access to the Framework 3.5, you could do something like this: Enum.GetValue

用空格在ComboBox中显示枚举

我有一个枚举,例如: enum MyEnum { My_Value_1, My_Value_2 } 随着: comboBox1.DataSource = Enum.GetValues(typeof(MyEnum)); 但现在我的问题是:如何用“”替换“_”,以便它变成空格而不是下划线的项目? 而且一个数据绑定对象仍然有效 如果你有权访问Framework 3.5,你可以这样做: Enum.GetValues(typeof(MyEnum)) .Cast<MyEnum>() .Select(e=> new { Value =

c# Get all enum values greater than a given value?

I have the following enumeration of membership roles: public enum RoleName { RegisteredUser, Moderator, Administrator, Owner } I want to be able to fetch all roles greater than or equal to a given role. For instance I input Administrator and I get an IEnumerable with RoleName.Administration and RoleName.Owner Something of this sort: public static void AddUserToRole(string u

c#获取大于给定值的所有枚举值?

我有以下列举的成员角色: public enum RoleName { RegisteredUser, Moderator, Administrator, Owner } 我希望能够获取大于或等于给定角色的所有角色。 例如我输入管理员,我得到一个与RoleName.Administration和RoleName.Owner的IEnumerable 这种东西: public static void AddUserToRole(string username, RoleName level) { var roles = Enum.GetValues(typeof(RoleName)).Cast<R>().ToList