2
0

StandardRuntimeFlagEnumValidatorAttribute.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. using System;
  7. using System.Configuration;
  8. [AttributeUsage(AttributeTargets.Property)]
  9. internal sealed class StandardRuntimeFlagEnumValidatorAttribute : ConfigurationValidatorAttribute
  10. {
  11. Type enumType;
  12. Type validatorType;
  13. public StandardRuntimeFlagEnumValidatorAttribute(Type enumType)
  14. {
  15. StandardRuntimeFlagEnumValidatorAttribute.ValidateFlagEnumType(enumType);
  16. this.EnumType = enumType;
  17. }
  18. public Type EnumType
  19. {
  20. get { return this.enumType; }
  21. set
  22. {
  23. StandardRuntimeFlagEnumValidatorAttribute.ValidateFlagEnumType(value);
  24. this.enumType = value;
  25. }
  26. }
  27. static bool IsPowerOfTwo(int value)
  28. {
  29. return value > 0 && (value & (value - 1)) == 0;
  30. }
  31. internal static void ValidateFlagEnumType(Type value)
  32. {
  33. if (value == null)
  34. {
  35. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("EnumType");
  36. }
  37. bool hasFlags = value.GetCustomAttributes(typeof(FlagsAttribute), true).Length > 0;
  38. if (!value.IsEnum || !hasFlags)
  39. {
  40. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("EnumType", SR.GetString(SR.FlagEnumTypeExpected, value));
  41. }
  42. int[] values = (int[])Enum.GetValues(value);
  43. if (values != null &&
  44. values.Length > 0)
  45. {
  46. for (int i = 0; i < values.Length; i++)
  47. {
  48. if (values[i] != 0 && !IsPowerOfTwo(values[i]))
  49. {
  50. if (!StandardRuntimeFlagEnumValidatorAttribute.IsCombinedValue(values[i], values, i - 1))
  51. {
  52. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("EnumType", SR.GetString(SR.InvalidFlagEnumType));
  53. }
  54. }
  55. }
  56. }
  57. }
  58. internal static bool IsCombinedValue(int combinedValue, int[] allowedValues, int startPosition)
  59. {
  60. int n = startPosition;
  61. while (n >= 0 &&
  62. combinedValue > 0)
  63. {
  64. if ((combinedValue & allowedValues[n]) == allowedValues[n])
  65. {
  66. combinedValue -= allowedValues[n];
  67. }
  68. n--;
  69. }
  70. return combinedValue == 0;
  71. }
  72. private void EnsureValidatorType()
  73. {
  74. if (this.validatorType == null)
  75. {
  76. validatorType = typeof(StandardRuntimeFlagEnumValidator<>).MakeGenericType(new System.Type[] { this.enumType });
  77. }
  78. }
  79. public override ConfigurationValidatorBase ValidatorInstance
  80. {
  81. get
  82. {
  83. this.EnsureValidatorType();
  84. return (ConfigurationValidatorBase)Activator.CreateInstance(validatorType, null);
  85. }
  86. }
  87. }
  88. }