如何重构该代码?
public enum enum1
{
value1 = 0x01,
value2 = 0x02,
value3 = 0x03,
value4 = 0x04,
value5 = 0x05,
UNKNOWN = 0xFF
}
class class1
{
private const string STR_VALUE1 = "some text description of value1";
private const string STR_VALUE2 = "some text description of value2";
private const string STR_VALUE3 = "some text description of value3";
private const string STR_VALUE4 = "some text description of value4";
private const string STR_VALUE5 = "some text description of value5";
private const string STR_VALUE6 = "some text description of unknown type";
public static string GetStringByTypeCode(enum1 type)
{
switch(type)
{
case enum1.value1:
return STR_VALUE1;
case enum1.value2:
return STR_VALUE2;
case enum1.value3:
return STR_VALUE3;
case enum1.value4:
return STR_VALUE4;
case enum1.value5:
return STR_VALUE5;
default:
return STR_VALUE6;
}
}
}
PS :有很多enum1 ... enumX和GetStringByTypeCode(enum1)... GetStringByTypeCode(enumX)方法。
编辑:我以这种方式重构:
namespace ConsoleApplication4
{
public enum enum1
{
value1 = 0x01,
value2 = 0x02,
value3 = 0x03,
value4 = 0x04,
value5 = 0x05,
UNKNOWN = 0xFF
}
class class1
{
static Dictionary<enum1, string> _dict;
static class1()
{
_dict = new Dictionary<enum1, string>();
_dict.Add(enum1.value1, "some text description of value1");
_dict.Add(enum1.value2, "some text description of value2");
_dict.Add(enum1.value3, "some text description of value3");
_dict.Add(enum1.value4, "some text description of value4");
_dict.Add(enum1.value5, "some text description of value5");
_dict.Add(enum1.UNKNOWN, "some text description of unknown type");
}
public static string GetStringByTypeCode(enum1 type)
{
string result = string.Empty;
try
{
_dict.TryGetValue(type, out result);
}
catch
{
}
return result;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(class1.GetStringByTypeCode(enum1.value4));
Console.ReadKey();
}
}
}
答案 0 :(得分:2)
您始终可以将枚举映射到Dictionary<enum1, string>
中的字符串,然后通过基于枚举键在字典中查找正确的字符串来实现您的方法。
答案 1 :(得分:1)
您可以重构为Dictionary<int,string>
。
如果您想保留enum
及其含义,请使用Dictionary<enum1,string>
。
// private field
private Dictionary<enum1,string> myDictionary = new Dictionary<enum1,string>();
// in constructor / other method
myDictionary.Add(enum1.value1, "some text description of value1");
myDictionary.Add(enum1.value2, "some text description of value2");
myDictionary.Add(enum1.value3, "some text description of value3");
myDictionary.Add(enum1.value4, "some text description of value4");
myDictionary.Add(enum1.value5, "some text description of value5");
myDictionary.Add(enum1.UNKNOWN, "some text description of unknown type");
然后实现简单的查找:
public string GetStringByTypeCode(enum1 type)
{
return myDictionary[type];
}
答案 2 :(得分:1)
另一个角度可能是使用DescriptionAttribute并创建一个帮助方法来为您检索描述。这是一些额外的工作,但允许您将描述直接映射到枚举值,而无需维护列表,如:
public enum Enum1
{
[Description("This is value 1")]
value1 = 0x001,
[Description("This is value 2")]
value2 = 0x002,
[Description("This is value 3")]
value3 = 0x003
}
....
public static class EnumExtensions
{
public static string GetDescription(this Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
}
}
然后,当您需要枚举的描述时,您只需执行以下操作:
Enum1 value = Emum1.Value1;
string valueDesc = value.GetDescription();
答案 3 :(得分:0)
使用您需要的数据和操作创建一个State-class。让你的州继承它。
abstract class State
{
public string Description { get; set; }
public void Behaviour();
}
您可以在其构造函数中初始化状态的描述。
public class MyClass
{
State s;
public MyClass(enum1 type)
{
switch(type)
{
case enum1.value1:
s = State1();
break;
case enum1.value2:
s = State2();
break;
...
}
}
}
您还可以使用Map将枚举映射到状态对象。 (Dictionary<enum1, State>
)
如果在状态对象中有不同的行为而不是不同的数据,则状态模式更有意义。因此,如果您想将Enum映射到String,您可以使用,如上所述,字典。