一、Enum的定义
public
enum
UserRolesType
![ExpandedBlockStart.gif](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...
{
UnKnown=0,
BaseSimple=70,
BaseBasic=71,
BaseExtend=72,
BaseBasic2=88,
BaseSimple2=89,
BaseExtend2=90
}
方法一:
根据ID获取枚举对象
protected
UserRolesType GetEnum(
int
t)
![ExpandedBlockStart.gif](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...
{
bool isInEnum = false;
UserRolesType c = UserRolesType.UnKnown;
if (t > 0)
![ExpandedSubBlockStart.gif](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![InBlock.gif](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach(int i in Enum.GetValues(typeof(UserRolesType)))
![ExpandedSubBlockStart.gif](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if (i == t)
![ExpandedSubBlockStart.gif](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
//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)
![ExpandedSubBlockStart.gif](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return UserRolesType.UnKnown;
}
![InBlock.gif](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
return c;
![InBlock.gif](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
方法二:根据ID获取枚举名称
protected
string
GetEnumName(
int
s)
![ExpandedBlockStart.gif](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...
{
string str=Enum.GetName(typeof(UserRolesType), s);
if (str == null)
![ExpandedSubBlockStart.gif](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
str = UserRolesType.UnKnown.ToString();
}
return str;
}
转载于:https://www.cnblogs.com/downmoon/archive/2007/12/29/1020067.html