public sealed override Array GetEnumValues() { if (!IsEnum) { throw new ArgumentException(SR.Arg_MustBeEnum, "enumType"); } Array values = Enum.GetEnumInfo(this).ValuesAsUnderlyingType; int count = values.Length; // Without universal shared generics, chances are slim that we'll have the appropriate // array type available. Offer an escape hatch that avoids a MissingMetadataException // at the cost of a small appcompat risk. Array result; if (AppContext.TryGetSwitch("Switch.System.Enum.RelaxedGetValues", out bool isRelaxed) && isRelaxed) { result = Array.CreateInstance(Enum.InternalGetUnderlyingType(this), count); } else { result = Array.CreateInstance(this, count); } Array.Copy(values, result, values.Length); return(result); }
public sealed override bool IsEnumDefined(object value) { if (value == null) { throw new ArgumentNullException(nameof(value)); } if (!IsEnum) { throw new ArgumentException(SR.Arg_MustBeEnum); } if (value is string valueAsString) { EnumInfo enumInfo = Enum.GetEnumInfo(this); foreach (string name in enumInfo.Names) { if (valueAsString == name) { return(true); } } return(false); } else { ulong rawValue; if (!Enum.TryGetUnboxedValueOfEnumOrInteger(value, out rawValue)) { if (Type.IsIntegerType(value.GetType())) { throw new ArgumentException(SR.Format(SR.Arg_EnumUnderlyingTypeAndObjectMustBeSameType, value.GetType(), Enum.InternalGetUnderlyingType(this))); } else { throw new InvalidOperationException(SR.InvalidOperation_UnknownEnumType); } } if (value is Enum) { if (!Enum.ValueTypeMatchesEnumType(this, value)) { throw new ArgumentException(SR.Format(SR.Arg_EnumAndObjectMustBeSameType, value.GetType(), this)); } } else { Type underlyingType = Enum.InternalGetUnderlyingType(this); if (!(underlyingType.TypeHandle.ToEETypePtr() == value.EETypePtr)) { throw new ArgumentException(SR.Format(SR.Arg_EnumUnderlyingTypeAndObjectMustBeSameType, value.GetType(), underlyingType)); } } return(Enum.GetEnumName(this, rawValue) != null); } }
public sealed override Array GetEnumValuesAsUnderlyingType() { if (!IsActualEnum) { throw new ArgumentException(SR.Arg_MustBeEnum, "enumType"); } return((Array)Enum.GetEnumInfo(this).ValuesAsUnderlyingType.Clone()); }