[英]C# Switch on enums
I have an enum: 我有一个枚举:
public enum Status
{
Incomplete = 1, Complete = 2, Cancelled = 3, Deleted = 4
}
Now on a certain page I wish to list this enum in a checkboxlist. 现在在某个页面上,我希望在复选框列表中列出此枚举。 This would be fine except that I want the text of each checkbox to display different text than the enum. 这很好,除了我希望每个复选框的文本显示不同于枚举的文本。
ie the check boxes should say: 即复选框应说:
"Not Processed" instead of "Incomplete" “未处理”而不是“不完整”
"Processed" instead of "Complete" “已处理”而非“完成”
"Void" instead of "Cancelled" “无效”而不是“取消”
Is it possible to put this enum in a foreach and then switch on the status and update the text. 是否可以将此枚举放在foreach中,然后打开状态并更新文本。 Like so: 像这样:
var statuses = Enum.GetNames(typeof(Status));
foreach (var status in statuses)))
{
switch (status)
{
case Status.Complete.ToString():
status = "Processed";
break; ...etc
}
}
Any ideas would be greatly appreciated. 任何想法将不胜感激。
C# has a language feature that directly addresses your question. C#具有直接解决您问题的语言功能。 Here's an article that gives you the complete details. 这是一篇文章 ,为您提供完整的详细信息。
The short version: apply extension attribute values to each value: 简短版本:将扩展属性值应用于每个值:
enum OrderStatus
{
[EnumValueData(Name="New Order")]
NewOrder = 1,
[EnumValueData(Name="In Process")]
Processing = 2,
[EnumValueData(Name="Shipped")]
Shipped = 3
}
Then apply said values to your radio-buttons, listbox or whatever: 然后将所述值应用于您的单选按钮,列表框或其他内容:
public static Dictionary<int, string> GetListItems(Type enumType)
{
if (!enumType.IsEnum)
throw new ApplicationException("GetListItems does not support non-enum types");
Dictionary<int, string> list = new Dictionary<int, string>();
foreach(FieldInfo field in enumType.GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public))
{
int value;
string display;
value = (int)field.GetValue(null);
display = Enum.GetName(enumType, value);
foreach(Attribute currAttr in field.GetCustomAttributes(true))
{
EnumValueDataAttribute valueAttribute = currAttr as EnumValueDataAttribute;
if (valueAttribute != null)
display = valueAttribute.Name;
}
list.Add(value, display);
}
return list;
}
You can use the Description attribute in the System.ComponentModel class to assign a text description to each enum value. 您可以使用System.ComponentModel类中的Description属性为每个枚举值分配文本描述。 You can then set the text property by iterating through the enum and getting the Description attribute of each instead of using a switch. 然后,您可以通过遍历枚举并获取每个的Description属性而不是使用开关来设置text属性。
public enum Status
{
[Description("Not Processed")] Incomplete = 1,
[Description("Processed")] Complete = 2,
[Description("Void")] Cancelled = 3,
Deleted = 4
}
You could then use something like the following to get the description of a single enum value (or modify it to return a Dictionary of as Bob showed in his example): 然后,您可以使用类似下面的内容来获取单个枚举值的描述(或修改它以返回Bob在其示例中显示的Dictionary):
public static string GetEnumDescription ( Object value )
{
try
{
Type objType = value.GetType();
FieldInfo fldInf = objType.GetField( Enum.GetName( objType, value ) );
Object[ ] attributes = fldInf.GetCustomAttributes( false );
if ( attributes.Length > 0 )
{
DescriptionAttribute descAttr = ( DescriptionAttribute )attributes[ 0 ];
return descAttr.Description;
}
else
{
return value.ToString();
}
}
catch
{
return string.Empty;
}
}
您可以将修改字符串放在一个数组中,然后使用枚举索引来访问数组元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.