2007年6月8日 星期五
enum description
When i make an enum like following
public enum SalaryType { ByHour, ByMonth };
I ususaly need to describe the meaning by text. So I need the following code
switch( employee.EmployeeType)
{
case Employee.ByHour:
Console.WriteLine("By hour");
break;
....
}
the code was seperated by definition and client code.
I found some code , and it so useful
public enum SalaryType {
[Description("By hour')]ByHour,
[Description("By month")] ByMonth };
public static string GetDescription(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();
}
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言