枚举类型(也称为枚举)是非常有意思的的一种值类型,它不仅可以提高程序的可读性,而且可以减少因业务改变而导致的程序改动。
另外枚举类型是强类型,接受方法必须有一个相同的匹配参数,否则将会出错。
好,废话少说,至于枚举类型(emun)的特性,几乎会编程的人都懂的,偶没功力在这里长篇大论。
下面是一个例子,偶还是按官方的写法,以星期为例子:
代码
class
Program
{
static void Main( string [] args)
{
Console.Write(GetDays(Days.Saturday));
Console.Read();
}
static string GetDays(Days _days)
{
string d = string .Empty;
switch (_days)
{
case Days.Monday: d = " Today is Monday. " ; break ;
case Days.Tuesday: d = " Today is Tuesday. " ; break ;
case Days.Wednesday: d = " Today is Wednesday. " ; break ;
case Days.Thursday: d = " Today is Thursday. " ; break ;
case Days.Friday: d = " Today is Friday. " ; break ; ;
case Days.Saturday: d = " Today is Saturday. " ; break ;
case Days.Sunday: d = " Today is Sunday. " ; break ;
}
return d;
}
}
public enum Days
{
///
/// this is sunday
///
Sunday = 7 ,
///
/// this is monday
///
Monday = 1 ,
///
/// this is tuesday
///
Tuesday = 2 ,
///
/// this is wednesday
///
Wednesday = 3 ,
///
/// this is thursday
///
Thursday = 4 ,
///
/// this is friday
///
Friday = 5 ,
///
/// this is saturday
///
Saturday = 6
};
{
static void Main( string [] args)
{
Console.Write(GetDays(Days.Saturday));
Console.Read();
}
static string GetDays(Days _days)
{
string d = string .Empty;
switch (_days)
{
case Days.Monday: d = " Today is Monday. " ; break ;
case Days.Tuesday: d = " Today is Tuesday. " ; break ;
case Days.Wednesday: d = " Today is Wednesday. " ; break ;
case Days.Thursday: d = " Today is Thursday. " ; break ;
case Days.Friday: d = " Today is Friday. " ; break ; ;
case Days.Saturday: d = " Today is Saturday. " ; break ;
case Days.Sunday: d = " Today is Sunday. " ; break ;
}
return d;
}
}
public enum Days
{
///
/// this is sunday
///
Sunday = 7 ,
///
/// this is monday
///
Monday = 1 ,
///
/// this is tuesday
///
Tuesday = 2 ,
///
/// this is wednesday
///
Wednesday = 3 ,
///
/// this is thursday
///
Thursday = 4 ,
///
/// this is friday
///
Friday = 5 ,
///
/// this is saturday
///
Saturday = 6
};
很明显,"Today is Saturday." 当然是输出结果,可问题来了,这只是一个枚举,7条枚举值记录,假如,我要改成:hi ,cora ! this is saturday. 然后其他另外6条都不一样的,假设我们有100个或者更多这样的枚举值(只是里面的值)要更改,业务层方法GetDays这样的方法岂不是大改?(当然这只是假设,如果真实的话,或许会有更灵活的方式去处理)
晕了,小菜的又开始说BT了...
然后,我把程序稍稍改了一下,加了一个公共的类RemarkAttribute的类,然后加了一个remark的属性:
代码
public
class
RemarkAttribute : Attribute
{
private string _remark;
public RemarkAttribute( string _remark)
{
this ._remark = _remark;
}
public string Remark
{
get { return _remark; }
set { _remark = value; }
}
public static string GetEnumRemark(System.Enum _enum)
{
Type type = _enum.GetType();
FieldInfo fd = type.GetField(_enum.ToString());
if (fd == null ) return string .Empty;
object [] attrs = fd.GetCustomAttributes( typeof (RemarkAttribute), false );
string name = string .Empty;
foreach (RemarkAttribute attr in attrs)
{
name = attr.Remark;
}
return name;
}
}
{
private string _remark;
public RemarkAttribute( string _remark)
{
this ._remark = _remark;
}
public string Remark
{
get { return _remark; }
set { _remark = value; }
}
public static string GetEnumRemark(System.Enum _enum)
{
Type type = _enum.GetType();
FieldInfo fd = type.GetField(_enum.ToString());
if (fd == null ) return string .Empty;
object [] attrs = fd.GetCustomAttributes( typeof (RemarkAttribute), false );
string name = string .Empty;
foreach (RemarkAttribute attr in attrs)
{
name = attr.Remark;
}
return name;
}
}
然后把GetDays方法去掉,并且枚举加上一个属性并且加上一个属性值:
代码
class
Program
{
static void Main( string [] args)
{
Console.Write(RemarkAttribute.GetEnumRemark(Days.Wednesday));
Console.Read();
}
}
public enum Days
{
///
/// this is sunday
///
[Remark( " this is sunday " )]
Sunday = 7 ,
///
/// this is monday
///
[Remark( " this is monday " )]
Monday = 1 ,
///
/// this is tuesday
///
[Remark( " this is tuesday " )]
Tuesday = 2 ,
///
/// this is wednesday
///
///
[Remark( " this is wednesday " )]
Wednesday = 3 ,
///
/// this is thursday
///
[Remark( " this is thursday " )]
Thursday = 4 ,
///
/// this is friday
///
[Remark( " this is friday " )]
Friday = 5 ,
///
/// this is saturday
///
[Remark( " this is saturday " )]
Saturday = 6
};
{
static void Main( string [] args)
{
Console.Write(RemarkAttribute.GetEnumRemark(Days.Wednesday));
Console.Read();
}
}
public enum Days
{
///
/// this is sunday
///
[Remark( " this is sunday " )]
Sunday = 7 ,
///
/// this is monday
///
[Remark( " this is monday " )]
Monday = 1 ,
///
/// this is tuesday
///
[Remark( " this is tuesday " )]
Tuesday = 2 ,
///
/// this is wednesday
///
///
[Remark( " this is wednesday " )]
Wednesday = 3 ,
///
/// this is thursday
///
[Remark( " this is thursday " )]
Thursday = 4 ,
///
/// this is friday
///
[Remark( " this is friday " )]
Friday = 5 ,
///
/// this is saturday
///
[Remark( " this is saturday " )]
Saturday = 6
};
好了,原来的业务处理方法GetDays可以省掉了,假设(偶好象比较喜欢假设),假设我们有100个或者更多个这样的枚举值要更改,那么我们只要把Remark的属性值改一下就可以了,业务层完全不用动(当然,连整个枚举都要更改的话,我就没办法喽)。
比较简单吧?个人而言还是比较喜欢简单而又不失经典的编码方式,或者园子里的朋友有更好的方式,不防提出来分享一下吧。