internal static void ValidateFlagEnumType(Type value) { if (value == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("EnumType"); } bool hasFlags = value.GetCustomAttributes(typeof(FlagsAttribute), true).Length > 0; if (!value.IsEnum || !hasFlags) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("EnumType", SR.GetString(SR.FlagEnumTypeExpected, value)); } int[] values = (int[])Enum.GetValues(value); if (values != null && values.Length > 0) { for (int i = 0; i < values.Length; i++) { if (values[i] != 0 && !IsPowerOfTwo(values[i])) { if (!StandardRuntimeFlagEnumValidatorAttribute.IsCombinedValue(values[i], values, i - 1)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("EnumType", SR.GetString(SR.InvalidFlagEnumType)); } } } } }
public override void Validate(object value) { if (!Enum.IsDefined(typeof(TEnum), value)) { TEnum dummy; if (!Enum.TryParse <TEnum>(value.ToString(), true, out dummy)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidEnumArgumentException("value", (int)value, typeof(TEnum))); } int combinedValue = (int)((object)dummy); int[] values = (int[])Enum.GetValues(typeof(TEnum)); if (!StandardRuntimeFlagEnumValidatorAttribute.IsCombinedValue(combinedValue, values, values.Length - 1)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidEnumArgumentException("value", (int)value, typeof(TEnum))); } } }