试试这个:
public enum t
{
[Description("/t:Min")] // <<---- set the string equivalent of the enum HERE.
min = 0, // <<---- set the return value of the enum HERE.
[Description("/t:Med")]
med = 1000,
[Description("/t:Max")]
max = 2000
}
您还需要这个方便的小类:
public static class EnumHelper
{
public static list_of_t = Enum.GetValues(typeof(t)).Cast<t>().ToList();
// returns the string description of the enum.
public static string GetEnumDescription(this Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
}
您可以使用这样的装饰枚举来获得所需的内容:
public int Option(string arg)
{
// get the matching enum for the "/t:" switch here.
var foundItem =
EnumHelper.list_of_t.SingleOrDefault(c => c.GetEnumDescription() == arg);
if (foundItem != null)
{
return (int)foundItem; // <<-- this will return 0, 1000, or 2000 in this example.
}
else
{
return 0;
}
}
HTH...