Exemple #1
0
        internal static String GetEnumName(RuntimeType eT, ulong ulValue)
        {
            Contract.Requires(eT != null);
            ulong[] ulValues = Enum.InternalGetValues(eT);
            int     index    = Array.BinarySearch(ulValues, ulValue);

            if (index >= 0)
            {
                string[] names = Enum.InternalGetNames(eT);
                return(names[index]);
            }

            return(null); // return null so the caller knows to .ToString() the input
        }
Exemple #2
0
        public override bool IsEnumDefined(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            // Check if both of them are of the same type
            RuntimeType valueType = (RuntimeType)value.GetType();

            // If the value is an Enum then we need to extract the underlying value from it
            if (valueType.IsEnum)
            {
                if (!valueType.IsEquivalentTo(this))
                {
                    throw new ArgumentException(SR.Format(SR.Arg_EnumAndObjectMustBeSameType, valueType, this));
                }

                valueType = (RuntimeType)valueType.GetEnumUnderlyingType();
            }

            // If a string is passed in
            if (valueType == StringType)
            {
                // Get all of the Fields, calling GetHashEntry directly to avoid copying
                string[] names = Enum.InternalGetNames(this);
                return(Array.IndexOf(names, value) >= 0);
            }

            // If an enum or integer value is passed in
            if (IsIntegerType(valueType))
            {
                RuntimeType underlyingType = Enum.InternalGetUnderlyingType(this);
                if (underlyingType != valueType)
                {
                    throw new ArgumentException(SR.Format(SR.Arg_EnumUnderlyingTypeAndObjectMustBeSameType, valueType, underlyingType));
                }

                ulong[] ulValues = Enum.InternalGetValues(this);
                ulong   ulValue  = Enum.ToUInt64(value);

                return(Array.BinarySearch(ulValues, ulValue) >= 0);
            }
            else
            {
                throw new InvalidOperationException(SR.InvalidOperation_UnknownEnumType);
            }
        }
Exemple #3
0
        public override Array GetEnumValues()
        {
            if (!IsActualEnum)
            {
                throw new ArgumentException(SR.Arg_MustBeEnum, "enumType");
            }

            // Get all of the values
            ulong[] values = Enum.InternalGetValues(this);

            // Create a generic Array
            Array ret = Array.CreateInstance(this, values.Length);

            for (int i = 0; i < values.Length; i++)
            {
                object val = Enum.ToObject(this, values[i]);
                ret.SetValue(val, i);
            }

            return(ret);
        }
Exemple #4
0
        /// <summary>
        /// Retrieves an array of the values of the underlying type constants in a specified enumeration type.
        /// </summary>
        /// <remarks>
        /// This method can be used to get enumeration values when creating an array of the enumeration type is challenging.
        /// For example, <see cref="T:System.Reflection.MetadataLoadContext" /> or on a platform where runtime codegen is not available.
        /// </remarks>
        /// <returns>An array that contains the values of the underlying type constants in enumType.</returns>
        /// <exception cref="ArgumentException">
        /// Thrown when the type is not Enum
        /// </exception>
        public override Array GetEnumValuesAsUnderlyingType()
        {
            if (!IsActualEnum)
            {
                throw new ArgumentException(SR.Arg_MustBeEnum, "enumType");
            }

            // Get all of the values
            ulong[] values = Enum.InternalGetValues(this);

            switch (RuntimeTypeHandle.GetCorElementType(Enum.InternalGetUnderlyingType(this)))
            {
            case CorElementType.ELEMENT_TYPE_U1:
            {
                var ret = new byte[values.Length];
                for (int i = 0; i < values.Length; i++)
                {
                    ret[i] = (byte)values[i];
                }
                return(ret);
            }

            case CorElementType.ELEMENT_TYPE_U2:
            {
                var ret = new ushort[values.Length];
                for (int i = 0; i < values.Length; i++)
                {
                    ret[i] = (ushort)values[i];
                }
                return(ret);
            }

            case CorElementType.ELEMENT_TYPE_U4:
            {
                var ret = new uint[values.Length];
                for (int i = 0; i < values.Length; i++)
                {
                    ret[i] = (uint)values[i];
                }
                return(ret);
            }

            case CorElementType.ELEMENT_TYPE_U8:
            {
                return((Array)values.Clone());
            }

            case CorElementType.ELEMENT_TYPE_I1:
            {
                var ret = new sbyte[values.Length];
                for (int i = 0; i < values.Length; i++)
                {
                    ret[i] = (sbyte)values[i];
                }
                return(ret);
            }

            case CorElementType.ELEMENT_TYPE_I2:
            {
                var ret = new short[values.Length];
                for (int i = 0; i < values.Length; i++)
                {
                    ret[i] = (short)values[i];
                }
                return(ret);
            }

            case CorElementType.ELEMENT_TYPE_I4:
            {
                var ret = new int[values.Length];
                for (int i = 0; i < values.Length; i++)
                {
                    ret[i] = (int)values[i];
                }
                return(ret);
            }

            case CorElementType.ELEMENT_TYPE_I8:
            {
                var ret = new long[values.Length];
                for (int i = 0; i < values.Length; i++)
                {
                    ret[i] = (long)values[i];
                }
                return(ret);
            }

            default:
                throw new InvalidOperationException(SR.InvalidOperation_UnknownEnumType);
            }
        }