一、Enum的定义
public
enum
UserRolesType
...
{
UnKnown
=
0
,
BaseSimple
=
70
,
BaseBasic
=
71
,
BaseExtend
=
72
,
BaseBasic2
=
88
,
BaseSimple2
=
89
,
BaseExtend2
=
90
}
方法一:
根据ID获取枚举对象
protected
UserRolesTypeGetEnum(
int
t)
...
{
bool
isInEnum
=
false
;
UserRolesTypec
=
UserRolesType.UnKnown;
if
(t
>
0
)
...
{
foreach
(
int
i
in
Enum.GetValues(
typeof
(UserRolesType)))
...
{
if
(i
==
t)
...
{
//
this.Debug(t.ToString(),"");
c
=
(UserRolesType)Enum.Parse(
typeof
(UserRolesType),i.ToString());
isInEnum
=
true
;
return
c;
//
(Colors)Enum.Parse(typeof(Colors),"Red,Yellow");
}

}
if
(isInEnum
==
false
)
...
{
return
UserRolesType.UnKnown;
}


}
return
c;
}
方法二:根据ID获取枚举名称
protected
string
GetEnumName(
int
s)
...
{
string
str
=
Enum.GetName(
typeof
(UserRolesType),s);
if
(str
==
null
)
...
{
str
=
UserRolesType.UnKnown.ToString();
}
return
str;
}
本文介绍了如何通过两种方法处理枚举类型:一是根据ID获取枚举对象,二是根据ID获取枚举名称。这两种方法适用于.NET环境下的开发工作,能够帮助开发者更高效地管理和使用枚举类型。
331

被折叠的 条评论
为什么被折叠?



