枚举到整型的转换,扩展方法为枚举添加方法取得整数值
时间:2011-01-10 来源:无为是最高
代码
public enum OperateTypeEnum
{
Add =1,//添加
Edit=2,//修改,编辑
Delete = 3,//
}
public static class EnumHelp
{
public static int GetValue(this OperateTypeEnum operateTypeEnum)
{
Type type = operateTypeEnum.GetType();
List< KeyValuePair<int,OperateTypeEnum>> keyCodeList = (from s in Enum.GetNames(type)
let keyCode = Int32.Parse(Enum.Format(type, Enum.Parse(type, s), "d"))
let operateType = (OperateTypeEnum) Enum.Parse(type, s)
select new KeyValuePair<int, OperateTypeEnum>(keyCode, operateType)).ToList();
KeyValuePair<int, OperateTypeEnum> currenItem = keyCodeList.FirstOrDefault(obj => obj.Value == operateTypeEnum);
return currenItem.Key;
}
}
{
Add =1,//添加
Edit=2,//修改,编辑
Delete = 3,//
}
public static class EnumHelp
{
public static int GetValue(this OperateTypeEnum operateTypeEnum)
{
Type type = operateTypeEnum.GetType();
List< KeyValuePair<int,OperateTypeEnum>> keyCodeList = (from s in Enum.GetNames(type)
let keyCode = Int32.Parse(Enum.Format(type, Enum.Parse(type, s), "d"))
let operateType = (OperateTypeEnum) Enum.Parse(type, s)
select new KeyValuePair<int, OperateTypeEnum>(keyCode, operateType)).ToList();
KeyValuePair<int, OperateTypeEnum> currenItem = keyCodeList.FirstOrDefault(obj => obj.Value == operateTypeEnum);
return currenItem.Key;
}
}
相关阅读 更多 +