GetNonGenericEnumInfo() static private method

static private GetNonGenericEnumInfo ( Type enumType ) : NonGenericEnumInfo
enumType System.Type
return NonGenericEnumInfo
Exemplo n.º 1
0
        /// <summary>
        /// Retrieves the <see cref="EnumMember{TEnum}"/>s of the flags that compose <paramref name="value"/>.
        /// </summary>
        /// <param name="enumType">The enum type.</param>
        /// <param name="value">The flags enum value.</param>
        /// <returns>The <see cref="EnumMember{TEnum}"/>s of the flags that compose <paramref name="value"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="enumType"/> or <paramref name="value"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="enumType"/> is not an enum type
        /// -or-
        /// <paramref name="value"/> is of an invalid type.</exception>
        public static IEnumerable <EnumMember> GetFlagMembers(Type enumType, object value)
        {
            var info = NonGenericEnums.GetNonGenericEnumInfo(enumType);

            if (value == null && info.IsNullable)
            {
                return(new EnumMember[0]);
            }

            return(info.EnumInfo.GetFlagMembers(value));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts the string representation of one or more members or values of <paramref name="enumType"/> delimited with <paramref name="delimiter"/> to its respective value of type <paramref name="enumType"/>
        /// using the specified parsing enum formats. The parameter <paramref name="ignoreCase"/> specifies if the operation is case-insensitive.
        /// </summary>
        /// <param name="enumType">The enum type.</param>
        /// <param name="value">The enum members or values' string representation.</param>
        /// <param name="ignoreCase">Indicates if the operation is case-insensitive.</param>
        /// <param name="delimiter">The delimiter used to separate individual flags.</param>
        /// <param name="formats">The parsing enum formats.</param>
        /// <returns>The <paramref name="enumType"/> value that is represented by <paramref name="value"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="enumType"/> or <paramref name="value"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="enumType"/> is not an enum type
        /// -or-
        /// <paramref name="value"/> doesn't represent a member or value of <paramref name="enumType"/>
        /// -or-
        /// <paramref name="formats"/> contains an invalid value.</exception>
        /// <exception cref="OverflowException"><paramref name="value"/> is outside the range of the underlying type of <paramref name="enumType"/>.</exception>
        public static object ParseFlags(Type enumType, string value, bool ignoreCase, string delimiter, params EnumFormat[] formats)
        {
            var info = NonGenericEnums.GetNonGenericEnumInfo(enumType);

            if (string.IsNullOrEmpty(value) && info.IsNullable)
            {
                return(null);
            }

            return(info.EnumInfo.ParseFlags(value, ignoreCase, delimiter, formats));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Indicates whether <paramref name="value"/> is a valid flag combination of <paramref name="enumType"/>'s defined flags.
        /// </summary>
        /// <param name="enumType">The enum type.</param>
        /// <param name="value">The flags enum value.</param>
        /// <returns>Indication of whether <paramref name="value"/> is a valid flag combination of <paramref name="enumType"/>'s defined flags.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="enumType"/> or <paramref name="value"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="enumType"/> is not an enum type
        /// -or-
        /// <paramref name="value"/> is of an invalid type.</exception>
        public static bool IsValidFlagCombination(Type enumType, object value)
        {
            var info = NonGenericEnums.GetNonGenericEnumInfo(enumType);

            if (value == null && info.IsNullable)
            {
                return(true);
            }

            return(info.EnumInfo.IsValidFlagCombination(value));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Indicates if <paramref name="value"/> has all of the flags that are defined in <paramref name="enumType"/>.
        /// </summary>
        /// <param name="enumType">The enum type.</param>
        /// <param name="value">The flags enum value.</param>
        /// <returns>Indication if <paramref name="value"/> has all of the flags that are defined in <paramref name="enumType"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="enumType"/> or <paramref name="value"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="enumType"/> is not an enum type
        /// -or-
        /// <paramref name="value"/> is of an invalid type.</exception>
        public static bool HasAllFlags(Type enumType, object value)
        {
            var info = NonGenericEnums.GetNonGenericEnumInfo(enumType);

            if (value == null && info.IsNullable)
            {
                return(false);
            }

            return(info.EnumInfo.HasAllFlags(value));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Retrieves <paramref name="value"/>'s flags formatted with <paramref name="formats"/> and delimited with <paramref name="delimiter"/>
        /// or if empty returns the zero flag formatted with <paramref name="formats"/>.
        /// If <paramref name="value"/> is not a valid flag combination <c>null</c> is returned.
        /// </summary>
        /// <param name="enumType">The enum type.</param>
        /// <param name="value">The flags enum value.</param>
        /// <param name="delimiter">The delimiter to use to separate individual flags.</param>
        /// <param name="formats">The output formats to use.</param>
        /// <returns><paramref name="value"/>'s flags formatted with <paramref name="formats"/> and delimited with <paramref name="delimiter"/>
        /// or if empty returns the zero flag formatted with <paramref name="formats"/>.
        /// If <paramref name="value"/> is not a valid flag combination <c>null</c> is returned.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="enumType"/> or <paramref name="value"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="enumType"/> is not an enum type
        /// -or-
        /// <paramref name="value"/> is of an invalid type
        /// -or-
        /// <paramref name="formats"/> contains an invalid value.</exception>
        public static string FormatFlags(Type enumType, object value, string delimiter, params EnumFormat[] formats)
        {
            var info = NonGenericEnums.GetNonGenericEnumInfo(enumType);

            if (value == null && info.IsNullable)
            {
                return(null);
            }

            return(info.EnumInfo.FormatFlags(value, delimiter, formats));
        }
Exemplo n.º 6
0
        private static bool TryParseFlags(Type enumType, string?value, bool ignoreCase, string?delimiter, out object?result, ValueCollection <EnumFormat> formats)
        {
            var info = NonGenericEnums.GetNonGenericEnumInfo(enumType);

            if (info.IsNullable && string.IsNullOrEmpty(value))
            {
                result = null;
                return(true);
            }

            return(info.EnumCache.TryParseFlags(value, ignoreCase, delimiter, out result, formats));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Returns <paramref name="value"/> with all of it's flags toggled. Equivalent to the bitwise "xor" operator with <see cref="GetAllFlags(Type)"/>.
        /// </summary>
        /// <param name="enumType">The enum type.</param>
        /// <param name="value">The flags enum value.</param>
        /// <returns><paramref name="value"/> with all of it's flags toggled.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="enumType"/> or <paramref name="value"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="enumType"/> is not an enum type
        /// -or-
        /// <paramref name="value"/> is of an invalid type.</exception>
        public static object ToggleFlags(Type enumType, object value)
        {
            var info     = NonGenericEnums.GetNonGenericEnumInfo(enumType);
            var enumInfo = info.EnumInfo;

            if (value == null && info.IsNullable)
            {
                return(enumInfo.AllFlags);
            }

            return(enumInfo.ToggleFlags(value));
        }
Exemplo n.º 8
0
        private static object?ParseFlags(Type enumType, string?value, bool ignoreCase, string?delimiter, ValueCollection <EnumFormat> formats)
        {
            var info = NonGenericEnums.GetNonGenericEnumInfo(enumType);

            if (info.IsNullable && string.IsNullOrEmpty(value))
            {
                return(null);
            }

            Preconditions.NotNull(value, nameof(value));

            return(info.EnumCache.ParseFlags(value !, ignoreCase, delimiter, formats));
        }
Exemplo n.º 9
0
        public static object CombineFlags(Type enumType, object?value, object?otherFlags)
        {
            var info  = NonGenericEnums.GetNonGenericEnumInfo(enumType);
            var cache = info.EnumCache;

            if (info.IsNullable)
            {
                if (value == null)
                {
                    return(otherFlags == null?cache.CombineFlags(null, true) : cache.ToObject(otherFlags, EnumValidation.None));
                }
                if (otherFlags == null)
                {
                    return(cache.ToObject(value, EnumValidation.None));
                }
            }

            return(cache.CombineFlags(value !, otherFlags !));
        }
Exemplo n.º 10
0
        /// <summary>
        /// Combines the flags of <paramref name="value"/> and <paramref name="otherFlags"/>. Equivalent to the bitwise "or" operation.
        /// </summary>
        /// <param name="enumType">The enum type.</param>
        /// <param name="value">The flags enum value.</param>
        /// <param name="otherFlags">The other flags enum value.</param>
        /// <returns>Combination of <paramref name="value"/> with the flags in <paramref name="otherFlags"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="enumType"/>, <paramref name="value"/>, or <paramref name="otherFlags"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="enumType"/> is not an enum type
        /// -or-
        /// <paramref name="value"/> or <paramref name="otherFlags"/> is of an invalid type.</exception>
        public static object CombineFlags(Type enumType, object value, object otherFlags)
        {
            var info     = NonGenericEnums.GetNonGenericEnumInfo(enumType);
            var enumInfo = info.EnumInfo;

            if (info.IsNullable)
            {
                if (value == null)
                {
                    return(enumInfo.ToObject(otherFlags));
                }
                if (otherFlags == null)
                {
                    return(enumInfo.ToObject(value));
                }
            }

            return(enumInfo.CombineFlags(value, otherFlags));
        }
Exemplo n.º 11
0
        /// <summary>
        /// Indicates if <paramref name="value"/> has all of the flags that are in <paramref name="otherFlags"/>.
        /// </summary>
        /// <param name="enumType">The enum type.</param>
        /// <param name="value">The flags enum value.</param>
        /// <param name="otherFlags">The other flags enum value.</param>
        /// <returns>Indication if <paramref name="value"/> has all of the flags that are in <paramref name="otherFlags"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="enumType"/>, <paramref name="value"/>, or <paramref name="otherFlags"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="enumType"/> is not an enum type
        /// -or-
        /// <paramref name="value"/> or <paramref name="otherFlags"/> is of an invalid type.</exception>
        public static bool HasAllFlags(Type enumType, object value, object otherFlags)
        {
            var info     = NonGenericEnums.GetNonGenericEnumInfo(enumType);
            var enumInfo = info.EnumInfo;

            if (info.IsNullable)
            {
                if (value == null)
                {
                    return(!enumInfo.HasAnyFlags(otherFlags));
                }
                if (otherFlags == null)
                {
                    enumInfo.ToObject(value);
                    return(true);
                }
            }

            return(enumInfo.HasAllFlags(value, otherFlags));
        }
Exemplo n.º 12
0
        public static bool HasAllFlags(Type enumType, object?value, object?otherFlags)
        {
            var info  = NonGenericEnums.GetNonGenericEnumInfo(enumType);
            var cache = info.EnumCache;

            if (info.IsNullable)
            {
                if (value == null)
                {
                    return(otherFlags == null || !cache.HasAnyFlags(otherFlags));
                }
                if (otherFlags == null)
                {
                    cache.ToObject(value, EnumValidation.None);
                    return(true);
                }
            }

            return(cache.HasAllFlags(value !, otherFlags !));
        }
Exemplo n.º 13
0
        public static int GetFlagCount(Type enumType, object?value, object?otherFlags)
        {
            var info  = NonGenericEnums.GetNonGenericEnumInfo(enumType);
            var cache = info.EnumCache;

            if (info.IsNullable)
            {
                if (value == null)
                {
                    if (otherFlags != null)
                    {
                        cache.ToObject(otherFlags, EnumValidation.None);
                    }
                    return(0);
                }
                if (otherFlags == null)
                {
                    cache.ToObject(value, EnumValidation.None);
                    return(0);
                }
            }

            return(cache.GetFlagCount(value !, otherFlags !));
        }
Exemplo n.º 14
0
        public static bool IsValidFlagCombination(Type enumType, object?value)
        {
            var info = NonGenericEnums.GetNonGenericEnumInfo(enumType);

            return(value == null && info.IsNullable ? true : info.EnumCache.IsValidFlagCombination(value !));
        }
Exemplo n.º 15
0
        private static string?FormatFlags(Type enumType, object?value, string?delimiter, ValueCollection <EnumFormat> formats)
        {
            var info = NonGenericEnums.GetNonGenericEnumInfo(enumType);

            return(value == null && info.IsNullable ? null : info.EnumCache.FormatFlags(value !, delimiter, formats));
        }
Exemplo n.º 16
0
        /// <summary>
        /// Combines all of the flags of <paramref name="flags"/>.
        /// </summary>
        /// <param name="enumType">The enum type.</param>
        /// <param name="flags">The flags enum values.</param>
        /// <returns>Combination of all of the flags of <paramref name="flags"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="enumType"/> or <paramref name="flags"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="enumType"/> is not an enum type
        /// -or-
        /// <paramref name="flags"/> contains a value that is of an invalid type.</exception>
        public static object CombineFlags(Type enumType, IEnumerable <object> flags)
        {
            var info = NonGenericEnums.GetNonGenericEnumInfo(enumType);

            return(info.EnumInfo.CombineFlags(info.IsNullable ? flags?.Where(flag => flag != null) : flags));
        }
Exemplo n.º 17
0
 /// <summary>
 /// The <see cref="NonGenericEnumComparer"/> constructor.
 /// </summary>
 /// <param name="enumType">The enum type.</param>
 /// <exception cref="ArgumentNullException"><paramref name="enumType"/> is <c>null</c>.</exception>
 /// <exception cref="ArgumentException"><paramref name="enumType"/> is not an enum type.</exception>
 public NonGenericEnumComparer(Type enumType)
 {
     _info = NonGenericEnums.GetNonGenericEnumInfo(enumType);
 }
Exemplo n.º 18
0
        public static object CombineFlags(Type enumType, IEnumerable <object?>?flags)
        {
            var info = NonGenericEnums.GetNonGenericEnumInfo(enumType);

            return(info.EnumCache.CombineFlags(flags, info.IsNullable));
        }
Exemplo n.º 19
0
        public static IEnumerable <EnumMember> GetFlagMembers(Type enumType, object?value)
        {
            var info = NonGenericEnums.GetNonGenericEnumInfo(enumType);

            return(value == null && info.IsNullable ? ArrayHelper.Empty <EnumMember>() : info.EnumCache.GetFlagMembers(value !));
        }
Exemplo n.º 20
0
        public static object ToggleFlags(Type enumType, object?value)
        {
            var info = NonGenericEnums.GetNonGenericEnumInfo(enumType);

            return(value == null && info.IsNullable ? info.EnumCache.GetAllFlags() : info.EnumCache.ToggleFlags(value !));
        }
Exemplo n.º 21
0
        public static int GetFlagCount(Type enumType, object?value)
        {
            var info = NonGenericEnums.GetNonGenericEnumInfo(enumType);

            return(value == null && info.IsNullable ? 0 : info.EnumCache.GetFlagCount(value !));
        }
Exemplo n.º 22
0
        public static bool HasAllFlags(Type enumType, object?value)
        {
            var info = NonGenericEnums.GetNonGenericEnumInfo(enumType);

            return(value == null && info.IsNullable ? false : info.EnumCache.HasAllFlags(value !));
        }