public static Dictionary <string, string> DictionaryOfValues(Type T, bool excludeDefault = true) { if (IsDescribedEnum(T)) { return(DictionaryOfValues(T.GetGenericArguments()[0], excludeDefault)); } Dictionary <string, string> res = new Dictionary <string, string>(); if (!T.IsEnum) { return(res); } foreach (object s in Enum.GetValues(T)) { if (excludeDefault) { DefaultValueAttribute va = AttrValue <DefaultValueAttribute>(s); if (va != null && ((va.Value as Boolean?) ?? false)) { continue; } } DatabaseCodeAttribute ca = AttrValue <DatabaseCodeAttribute>(s); DescriptionAttribute da = AttrValue <DescriptionAttribute>(s); string code = (ca == null)? s.ToString() : ca.Code; string name = (da == null)? s.ToString() : da.Description; res.Add(code, name); } return(res); }
public static object GetEnumValue(Type T, string code) { if (!T.IsEnum) { return(null); } string res = code; bool foundIt = false; foreach (object s in Enum.GetValues(T)) { DatabaseCodeAttribute ca = AttrValue <DatabaseCodeAttribute>(s); if (ca != null && ca.Code == code) { res = s.ToString(); foundIt = true; break; } } if (!foundIt && String.IsNullOrEmpty(code)) { return(GetDefaultValue(T)); } return(Enum.Parse(T, res)); }
public static string GetDatabaseCode(object val) { if (val == null) { return(""); } DatabaseCodeAttribute ca = AttrValue <DatabaseCodeAttribute>(val); if (ca != null) { return(ca.Code); } return(val.ToString()); }
public static bool IsValidCode(Type T, string code) { if (!T.IsEnum || code == null) { return(false); } string res = code; foreach (object s in Enum.GetValues(T)) { DatabaseCodeAttribute ca = AttrValue <DatabaseCodeAttribute>(s); string sName = s.ToString(); if (ca != null && ca.Code == code) { return(true); } else if (ca == null && code == sName) { return(true); } } return(false); }