Ejemplo n.º 1
0
        internal static object XmlToEnum(string value, Type type, Type attribType, bool requireValue = true, bool allowFlags = true, bool allowParse = true, bool allowNumeric = false)
        {
            var enumXmlCache = ReflectionCacheManager.GetEnumXml(type);

            var val = enumXmlCache.Cache.GetValue(value, attribType);

            if (val != null)
            {
                return(val);
            }

            if (!allowParse)
            {
                return(null);
            }

            try
            {
                var underlying = Nullable.GetUnderlyingType(type);

                if (underlying != null)
                {
                    type = underlying;
                }

                //todo: make this a tryparse, and then reparse and throw if we failed to parse

                var e = Enum.Parse(type, value, true);

                if (allowFlags == false && ((Enum)e).GetUnderlyingFlags().Any())
                {
                    return(null);
                }

                if (!allowNumeric && e.ToString().ToLower() != value.ToLower())
                {
                    return(null);
                }

                return(e);
            }
            catch
            {
                if (requireValue)
                {
                    throw;
                }

                return(null);
            }
        }
Ejemplo n.º 2
0
        internal static object XmlToEnum(string value, Type type, Type attribType, bool requireValue = true, bool allowFlags = true, bool allowParse = true)
        {
            var val = ReflectionCacheManager.GetEnumXml(type).GetValue(value, attribType);

            if (val != null)
            {
                return(val);
            }


            if (!allowParse)
            {
                return(null);
            }

            try
            {
                var underlying = Nullable.GetUnderlyingType(type);

                if (underlying != null)
                {
                    type = underlying;
                }

                var e = Enum.Parse(type, value, true);

                if (allowFlags == false && ((Enum)e).GetUnderlyingFlags().Any())
                {
                    return(null);
                }

                return(e);
            }
            catch
            {
                if (requireValue)
                {
                    throw;
                }

                return(null);
            }
        }