标签:stat int enum nat 使用 c# public sum logs
工作中 经常遇到枚举 的一些转换 特别是获取枚举备注等 特地整理下 方法以后使用
public void TestMethod1()
{
TestEnumOne colorEnum = TestEnumOne.Red;
int colorValue = 0x0000FF;
string colorStr = "Red";
string colorDes = "红色";
//枚举To枚举字符串
colorStr = colorEnum.ToString();
colorStr = Enum.GetName(typeof(TestEnumOne), colorEnum);
//枚举值To枚举字符串
colorStr = Enum.GetName(typeof(TestEnumOne), colorValue);
//枚举To枚举值
colorValue = colorEnum.GetHashCode();
colorValue = (int)colorEnum;
//枚举字符To枚举值
colorValue = Enum.Parse(typeof(TestEnumOne), colorStr).GetHashCode();
colorValue = (int)Enum.Parse(typeof(TestEnumOne), colorStr);
//枚举字To枚举
colorEnum = (TestEnumOne)Enum.Parse(typeof(TestEnumOne), colorStr);
//枚举值To枚举
colorEnum = (TestEnumOne)colorValue;
//根据枚举获取备注
colorDes = TestEnumOne.Red.GetEnumDescriptionByEnum(typeof(TestEnumOne));
//根据枚举值获取备注
colorDes = TestEnumOne.Blue.GetEnumDescriptionByEnumValue(typeof(TestEnumOne), colorValue);
//根据枚举字符串获取备注
colorDes = TestEnumOne.Blue.GetEnumDescriptionByEnumString(typeof(TestEnumOne), colorStr);
}
下面是一个 枚举 和上面用到的一些方法
public static class EnumClass { public enum TestEnumOne { [Description("红色")] Red = 0xff0000, [Description("橙色")] Orange = 0xFFA500, [Description("黄色")] Yellow = 0xFFFF00, [Description("蓝色")] Blue = 0x0000FF, } /// <summary> /// 根据枚举获取备注 /// </summary> /// <param name="aEnum">枚举</param> /// <param name="enumType">枚举类型</param> /// <returns>备注</returns> public static string GetEnumDescriptionByEnum(this Enum aEnum, System.Type enumType) { string enumDescription = string.Empty; foreach (System.Enum enumItem in System.Enum.GetValues(enumType)) { string enumString = Enum.GetName(enumType, aEnum); if (enumString.ToLower() == Enum.GetName(enumType, enumItem).ToLower()) { FieldInfo fi = enumType.GetField(enumString); DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, enumType); if (da != null) { enumDescription = da.Description; } } } return enumDescription; } /// <summary> /// 根据枚举值获取备注 /// </summary> /// <param name="aEnum">枚举</param> /// <param name="enumType">枚举类型</param> /// <param name="enumValue">枚举值</param> /// <returns>备注</returns> public static string GetEnumDescriptionByEnumValue(this Enum aEnum, System.Type enumType, int enumValue) { string enumDescription = string.Empty; foreach (System.Enum enumItem in System.Enum.GetValues(enumType)) { if (enumItem.GetHashCode() == (int)enumValue) { string enumString = Enum.GetName(enumType, enumValue); FieldInfo fi = enumType.GetField(enumString); DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, enumType); if (da != null) { enumDescription = da.Description; } } } return enumDescription; } /// <summary> /// 根据枚举字符串获取备注 /// </summary> /// <param name="aEnum">枚举</param> /// <param name="enumType">枚举类型</param> /// <param name="enumValue">枚举值</param> /// <returns>备注</returns> public static string GetEnumDescriptionByEnumString(this Enum aEnum, System.Type enumType, string enumString) { string enumDescription = string.Empty; foreach (System.Enum enumItem in System.Enum.GetValues(enumType)) { if (enumString.ToLower() == Enum.GetName(enumType, enumItem).ToLower()) { FieldInfo fi = enumType.GetField(enumString); DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, enumType); if (da != null) { enumDescription = da.Description; } } } return enumDescription; } }
标签:stat int enum nat 使用 c# public sum logs
原文地址:http://www.cnblogs.com/burg-xun/p/7091040.html