// Convert everything to ulong then perform a binary search. private static int BinarySearch(Array array, object value) { ulong[] ulArray = new ulong[array.Length]; for (int i = 0; i < array.Length; ++i) { ulArray[i] = Enum.ToUInt64(array.GetValue(i) !); } ulong ulValue = Enum.ToUInt64(value); return(Array.BinarySearch(ulArray, ulValue)); }
public override bool IsEnumDefined(object value) { if (value == null) { throw new ArgumentNullException(nameof(value)); } if (!IsEnum) { throw new ArgumentException(SR.Arg_MustBeEnum, "enumType"); } // 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); } }
public override string?GetEnumName(object value) { ArgumentNullException.ThrowIfNull(value); RuntimeType valueType = (RuntimeType)value.GetType(); if (!(valueType.IsActualEnum || IsIntegerType(valueType))) { throw new ArgumentException(SR.Arg_MustBeEnumBaseTypeOrEnum, nameof(value)); } ulong ulValue = Enum.ToUInt64(value); return(Enum.GetEnumName(this, ulValue)); }
public override string?GetEnumName(object value) { if (value == null) { throw new ArgumentNullException(nameof(value)); } Type valueType = value.GetType(); if (!(valueType.IsEnum || IsIntegerType(valueType))) { throw new ArgumentException(SR.Arg_MustBeEnumBaseTypeOrEnum, nameof(value)); } ulong ulValue = Enum.ToUInt64(value); return(Enum.GetEnumName(this, ulValue)); }