示例#1
0
        public static bool TryGetEnum <T>(string str, StringComparison comparisonType, out T result)
        {
            Dictionary <string, object> strs;
            object obj;
            bool   flag;
            Type   type = typeof(T);

            EnumUtils.s_enumCache.TryGetValue(type, out strs);
            if (strs != null && strs.TryGetValue(str, out obj))
            {
                result = (T)obj;
                return(true);
            }
            IEnumerator enumerator = Enum.GetValues(type).GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    T    current = (T)enumerator.Current;
                    bool flag1   = false;
                    if (!EnumUtils.GetString <T>(current).Equals(str, comparisonType))
                    {
                        FieldInfo field = current.GetType().GetField(current.ToString());
                        DescriptionAttribute[] customAttributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                        int num = 0;
                        while (num < (int)customAttributes.Length)
                        {
                            if (!customAttributes[num].Description.Equals(str, comparisonType))
                            {
                                num++;
                            }
                            else
                            {
                                flag1 = true;
                                break;
                            }
                        }
                    }
                    else
                    {
                        flag1  = true;
                        result = current;
                    }
                    if (!flag1)
                    {
                        continue;
                    }
                    if (strs == null)
                    {
                        strs = new Dictionary <string, object>();
                        EnumUtils.s_enumCache.Add(type, strs);
                    }
                    if (!strs.ContainsKey(str))
                    {
                        strs.Add(str, current);
                    }
                    result = current;
                    flag   = true;
                    return(flag);
                }
                result = default(T);
                return(false);
            }
            finally
            {
                IDisposable disposable  = enumerator as IDisposable;
                IDisposable disposable1 = disposable;
                if (disposable != null)
                {
                    disposable1.Dispose();
                }
            }
            return(flag);
        }
示例#2
0
 public static bool TryGetEnum <T>(string str, out T outVal)
 {
     return(EnumUtils.TryGetEnum <T>(str, StringComparison.Ordinal, out outVal));
 }
示例#3
0
 public static T GetEnum <T>(string str)
 {
     return(EnumUtils.GetEnum <T>(str, StringComparison.Ordinal));
 }
示例#4
0
        public static bool TryGetEnum <T>(string str, StringComparison comparisonType, out T result)
        {
            Type typeFromHandle = typeof(T);
            Dictionary <string, object> dictionary;

            EnumUtils.s_enumCache.TryGetValue(typeFromHandle, out dictionary);
            object obj;

            if (dictionary != null && dictionary.TryGetValue(str, out obj))
            {
                result = (T)((object)obj);
                return(true);
            }
            IEnumerator enumerator = Enum.GetValues(typeFromHandle).GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    object obj2    = enumerator.Current;
                    T      t       = (T)((object)obj2);
                    bool   flag    = false;
                    string @string = EnumUtils.GetString <T>(t);
                    if (@string.Equals(str, comparisonType))
                    {
                        flag   = true;
                        result = t;
                    }
                    else
                    {
                        FieldInfo field = t.GetType().GetField(t.ToString());
                        DescriptionAttribute[] array = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                        for (int i = 0; i < array.Length; i++)
                        {
                            if (array[i].Description.Equals(str, comparisonType))
                            {
                                flag = true;
                                break;
                            }
                        }
                    }
                    if (flag)
                    {
                        if (dictionary == null)
                        {
                            dictionary = new Dictionary <string, object>();
                            EnumUtils.s_enumCache.Add(typeFromHandle, dictionary);
                        }
                        if (!dictionary.ContainsKey(str))
                        {
                            dictionary.Add(str, t);
                        }
                        result = t;
                        return(true);
                    }
                }
            }
            finally
            {
                IDisposable disposable;
                if ((disposable = (enumerator as IDisposable)) != null)
                {
                    disposable.Dispose();
                }
            }
            result = default(T);
            return(false);
        }