The Labs \ Source Viewer \ SSCLI \ System.Configuration \ SubclassTypeValidator

  1. //------------------------------------------------------------------------------
  2. // <copyright file="SubclassTypeValidator.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. using System;
  16. using System.Collections;
  17. using System.IO;
  18. using System.Reflection;
  19. using System.Security.Permissions;
  20. using System.Xml;
  21. using System.Collections.Specialized;
  22. using System.Globalization;
  23. using System.ComponentModel;
  24. using System.Security;
  25. using System.Text;
  26. using System.Text.RegularExpressions;
  27. namespace System.Configuration
  28. {
  29.    
  30.     // This class validates that the value is a subclass of a specified type
  31.     public sealed class SubclassTypeValidator : ConfigurationValidatorBase
  32.     {
  33.         private Type _base;
  34.        
  35.         public SubclassTypeValidator(Type baseClass)
  36.         {
  37.             if (baseClass == null) {
  38.                 throw new ArgumentNullException("baseClass");
  39.             }
  40.            
  41.             _base = baseClass;
  42.         }
  43.        
  44.         public override bool CanValidate(Type type)
  45.         {
  46.             return (type == typeof(Type));
  47.         }
  48.        
  49.         public override void Validate(object value)
  50.         {
  51.             if (value == null) {
  52.                 return;
  53.             }
  54.            
  55.             // Make a check here since value.GetType() returns RuntimeType rather then Type
  56.             if (!(value is Type)) {
  57.                 ValidatorUtils.HelperParamValidation(value, typeof(Type));
  58.             }
  59.            
  60.             if (!_base.IsAssignableFrom((Type)value)) {
  61.                 throw new ArgumentException(SR.GetString(SR.Subclass_validator_error, ((Type)value).FullName, _base.FullName));
  62.             }
  63.         }
  64.     }
  65. }

Developer Fusion