public static object ToDbValue(Enum value) { if (value == null) { return(null); } BidirHashtable <object, EnumValueAttribute> map = EnumToAttributeMap(value.GetType()); return(map[value].DbValue); }
public static object DbValueToEnum <T>(object dbValue) { if (Convert.IsDBNull(dbValue)) { return(null); } else { BidirHashtable <object, object> map = EnumToDbValueMap(typeof(T)); return(map.ReverseLookup(dbValue)); } }
public static object DisplayValueToEnum <T>(object displayValue) { BidirHashtable <object, EnumValueAttribute> map = EnumToAttributeMap(typeof(T)); foreach (string e in Enum.GetNames(typeof(T))) { if (map[Enum.Parse(typeof(T), e)].DisplayValue.Equals(displayValue)) { return(Enum.Parse(typeof(T), e)); } } return(null); }
private static BidirHashtable <object, object> EnumToDbValueMap(Type enumType) { BidirHashtable <object, object> retval = new BidirHashtable <object, object>(); foreach (FieldInfo fi in enumType.GetFields()) { if (fi.FieldType.BaseType == typeof(Enum)) { EnumValueAttribute[] attrs = (EnumValueAttribute[])fi.GetCustomAttributes( typeof(EnumValueAttribute), false); if (attrs.Length > 0) { retval.Add(Enum.Parse(enumType, fi.Name), attrs[0].DbValue); } } } return(retval); }