示例#1
0
        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);
        }
示例#2
0
        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));
        }
示例#3
0
        public static string GetDatabaseCode(object val)
        {
            if (val == null)
            {
                return("");
            }

            DatabaseCodeAttribute ca = AttrValue <DatabaseCodeAttribute>(val);

            if (ca != null)
            {
                return(ca.Code);
            }

            return(val.ToString());
        }
示例#4
0
        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);
        }