ServiceModelEnumValidator.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. using System;
  7. using System.ComponentModel;
  8. using System.Configuration;
  9. using System.Reflection;
  10. using System.ServiceModel.Channels;
  11. internal class ServiceModelEnumValidator : ConfigurationValidatorBase
  12. {
  13. Type enumHelperType;
  14. MethodInfo isDefined;
  15. public ServiceModelEnumValidator(Type enumHelperType)
  16. {
  17. this.enumHelperType = enumHelperType;
  18. this.isDefined = this.enumHelperType.GetMethod("IsDefined", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
  19. }
  20. public override bool CanValidate(Type type)
  21. {
  22. return (this.isDefined != null);
  23. }
  24. public override void Validate(object value)
  25. {
  26. bool retVal = (bool)this.isDefined.Invoke(null, new object[] { value });
  27. if (!retVal)
  28. {
  29. ParameterInfo[] isDefinedParameters = this.isDefined.GetParameters();
  30. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidEnumArgumentException("value", (int)value, isDefinedParameters[0].ParameterType));
  31. }
  32. }
  33. }
  34. }