public ConfigurationProperty (
					string name, Type type, object default_value,
					TypeConverter converter,
					ConfigurationValidatorBase validation,
					ConfigurationPropertyOptions flags)
			: this (name, type, default_value, converter, validation, flags, null)
		{ }
        public ConfigurationElementProperty(ConfigurationValidatorBase validator) {
            if (validator == null) {
                throw new ArgumentNullException("validator");
            }

            _validator = validator;
        }
예제 #3
0
        public ElementMap(Type t)
        {
            properties = new ConfigurationPropertyCollection();

            collectionAttribute = Attribute.GetCustomAttribute(t, typeof(ConfigurationCollectionAttribute)) as ConfigurationCollectionAttribute;

            PropertyInfo[] props = t.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
            foreach (PropertyInfo prop in props)
            {
                ConfigurationPropertyAttribute at = Attribute.GetCustomAttribute(prop, typeof(ConfigurationPropertyAttribute)) as ConfigurationPropertyAttribute;
                if (at == null)
                {
                    continue;
                }
                string name = at.Name != null ? at.Name : prop.Name;

                ConfigurationValidatorAttribute validatorAttr = Attribute.GetCustomAttribute(prop, typeof(ConfigurationValidatorAttribute)) as ConfigurationValidatorAttribute;
                ConfigurationValidatorBase      validator     = validatorAttr != null ? validatorAttr.ValidatorInstance : null;

                TypeConverterAttribute convertAttr = (TypeConverterAttribute)Attribute.GetCustomAttribute(prop, typeof(TypeConverterAttribute));
                TypeConverter          converter   = convertAttr != null ? (TypeConverter)Activator.CreateInstance(Type.GetType(convertAttr.ConverterTypeName), true) : null;
                ConfigurationProperty  cp          = new ConfigurationProperty(name, prop.PropertyType, at.DefaultValue, converter, validator, at.Options);

                cp.CollectionAttribute = Attribute.GetCustomAttribute(prop, typeof(ConfigurationCollectionAttribute)) as ConfigurationCollectionAttribute;
                properties.Add(cp);
            }
        }
예제 #4
0
 private void ConstructorInit(string name, System.Type type, ConfigurationPropertyOptions options, ConfigurationValidatorBase validator, TypeConverter converter)
 {
     if (typeof(ConfigurationSection).IsAssignableFrom(type))
     {
         throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_properties_may_not_be_derived_from_configuration_section", new object[] { name }));
     }
     this._providedName = name;
     if (((options & ConfigurationPropertyOptions.IsDefaultCollection) != ConfigurationPropertyOptions.None) && string.IsNullOrEmpty(name))
     {
         name = DefaultCollectionPropertyName;
     }
     else
     {
         this.ValidatePropertyName(name);
     }
     this._name      = name;
     this._type      = type;
     this._options   = options;
     this._validator = validator;
     this._converter = converter;
     if (this._validator == null)
     {
         this._validator = DefaultValidatorInstance;
     }
     else if (!this._validator.CanValidate(this._type))
     {
         throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Validator_does_not_support_prop_type", new object[] { this._name }));
     }
 }
 public ConfigurationElementProperty(ConfigurationValidatorBase validator)
 {
     if (validator == null)
     {
         throw new ArgumentNullException("validator");
     }
     this._validator = validator;
 }
 public ConfigurationProperty(
     string name, Type type, object defaultValue,
     TypeConverter typeConverter,
     ConfigurationValidatorBase validator,
     ConfigurationPropertyOptions options)
     : this(name, type, defaultValue, typeConverter, validator, options, null)
 {
 }
 public ConfigurationProperty(String name,
                              Type type,
                              Object defaultValue,
                              TypeConverter typeConverter,
                              ConfigurationValidatorBase validator,
                              ConfigurationPropertyOptions options)
     : this(name, type, defaultValue, typeConverter, validator, options, null) {
 }
예제 #8
0
 public ConfigurationProperty(
     string name, Type type, object default_value,
     TypeConverter converter,
     ConfigurationValidatorBase validation,
     ConfigurationPropertyOptions flags)
     : this(name, type, default_value, converter, validation, flags, null)
 {
 }
        public ConfigurationProperty(String name,
                                     Type type,
                                     Object defaultValue,
                                     TypeConverter typeConverter,
                                     ConfigurationValidatorBase validator,
                                     ConfigurationPropertyOptions options,
                                     string description) {
            ConstructorInit(name, type, options, validator, typeConverter);

            SetDefaultValue(defaultValue);
        }
예제 #10
0
        public ConfigurationProperty(string name,
                                     Type type,
                                     object defaultValue,
                                     TypeConverter typeConverter,
                                     ConfigurationValidatorBase validator,
                                     ConfigurationPropertyOptions options,
                                     string description)
        {
            ConstructorInit(name, type, options, validator, typeConverter);

            SetDefaultValue(defaultValue);
        }
 public static void AddStringProperty(
     this ConfigurationPropertyCollection properties,
     string name,
     ConfigurationValidatorBase validator,
     ConfigurationPropertyOptions options = ConfigurationPropertyOptions.IsRequired)
 {
     // http://stackoverflow.com/questions/3744953/why-does-configurationvalidator-validate-the-default-value-of-a-configurationprop
     properties.Add(new ConfigurationProperty(
         name: name,
         type: typeof(string),
         defaultValue: null,
         typeConverter: null,
         validator: validator,
         options: options));
 }
예제 #12
0
        private void ConstructorInit(
            string name,
            Type type,
            ConfigurationPropertyOptions options,
            ConfigurationValidatorBase validator,
            TypeConverter converter,
            string description)
        {
            if (typeof(ConfigurationSection).IsAssignableFrom(type))
            {
                throw new ConfigurationErrorsException(
                          SR.Format(SR.Config_properties_may_not_be_derived_from_configuration_section, name));
            }

            // save the provided name so we can check for default collection names
            ProvidedName = name;

            if (((options & ConfigurationPropertyOptions.IsDefaultCollection) != 0) && string.IsNullOrEmpty(name))
            {
                name = DefaultCollectionPropertyName;
            }
            else
            {
                ValidatePropertyName(name);
            }

            Name        = name;
            Description = description;
            Type        = type;
            _options    = options;
            Validator   = validator;
            _converter  = converter;

            // Use the default validator if none was supplied
            if (Validator == null)
            {
                Validator = s_defaultValidatorInstance;
            }
            else
            {
                // Make sure the supplied validator supports the type of this property
                if (!Validator.CanValidate(Type))
                {
                    throw new ConfigurationErrorsException(SR.Format(SR.Validator_does_not_support_prop_type, Name));
                }
            }
        }
예제 #13
0
        public ConfigurationProperty(
            string name, Type type, object defaultValue,
            TypeConverter converter,
            ConfigurationValidatorBase validation,
            ConfigurationPropertyOptions flags,
            string description)
        {
            Name      = name;
            Converter = converter ?? TypeDescriptor.GetConverter(type);
            if (defaultValue != null)
            {
                if (defaultValue == NoDefaultValue)
                {
                    switch (Type.GetTypeCode(type))
                    {
                    case TypeCode.Object:
                        defaultValue = null;
                        break;

                    case TypeCode.String:
                        defaultValue = string.Empty;
                        break;

                    default:
                        defaultValue = Activator.CreateInstance(type);
                        break;
                    }
                }
                else if (!type.GetTypeInfo().IsAssignableFrom(defaultValue.GetType()))
                {
                    if (!Converter.CanConvertFrom(defaultValue.GetType()))
                    {
                        throw new ConfigurationErrorsException(
                                  string.Format(
                                      "The default value for property '{0}' has a different type than the one of the property itself: expected {1} but was {2}",
                                      name, type, defaultValue.GetType()));
                    }

                    defaultValue = Converter.ConvertFrom(defaultValue);
                }
            }
            DefaultValue = defaultValue;
            _flags       = flags;
            Type         = type;
            Validator    = validation ?? new DefaultValidator();
            Description  = description;
        }
        public ConfigurationProperty(
            string name, Type type, object defaultValue,
            TypeConverter typeConverter,
            ConfigurationValidatorBase validator,
            ConfigurationPropertyOptions options,
            string description)
        {
            this.name      = name;
            this.converter = typeConverter != null ? typeConverter : TypeDescriptor.GetConverter(type);
            if (defaultValue != null)
            {
                if (defaultValue == NoDefaultValue)
                {
                    switch (Type.GetTypeCode(type))
                    {
                    case TypeCode.Object:
                        defaultValue = null;
                        break;

                    case TypeCode.String:
                        defaultValue = String.Empty;
                        break;

                    default:
                        defaultValue = Activator.CreateInstance(type);
                        break;
                    }
                }
                else
                if (!type.IsAssignableFrom(defaultValue.GetType()))
                {
                    if (!this.converter.CanConvertFrom(defaultValue.GetType()))
                    {
                        throw new ConfigurationErrorsException(String.Format("The default value for property '{0}' has a different type than the one of the property itself: expected {1} but was {2}",
                                                                             name, type, defaultValue.GetType()));
                    }

                    defaultValue = this.converter.ConvertFrom(defaultValue);
                }
            }
            this.default_value = defaultValue;
            this.flags         = options;
            this.type          = type;
            this.validation    = validator != null ? validator : new DefaultValidator();
            this.description   = description;
        }
예제 #15
0
		public ConfigurationProperty (
					string name, Type type, object default_value,
					TypeConverter converter,
					ConfigurationValidatorBase validation,
					ConfigurationPropertyOptions flags,
					string description)
		{
			this.name = name;
			this.converter = converter != null ? converter : TypeDescriptor.GetConverter (type);
			if (default_value != null) {
				if (default_value == NoDefaultValue) {
					switch (Type.GetTypeCode (type)) {
					case TypeCode.Object:
						default_value = null;
						break;
					case TypeCode.String:
						default_value = String.Empty;
						break;
					default:
						default_value = Activator.CreateInstance (type);
						break;
					}
				}
				else
					if (!type.IsAssignableFrom (default_value.GetType ())) {
						if (!this.converter.CanConvertFrom (default_value.GetType ()))
							throw new ConfigurationErrorsException (String.Format ("The default value for property '{0}' has a different type than the one of the property itself: expected {1} but was {2}",
													   name, type, default_value.GetType ()));

						default_value = this.converter.ConvertFrom (default_value);
					}
			}
			this.default_value = default_value;
			this.flags = flags;
			this.type = type;
			this.validation = validation != null ? validation : new DefaultValidator ();
			this.description = description;
		}
예제 #16
0
        /// <summary>
        /// Initializes the <see cref="RouteElement"/> class.
        /// </summary>
        static RouteElement()
        {
            _nonEmptyStringValidator = new StringValidator(1);

            _propName = new ConfigurationProperty(
                "name", typeof (string), null, null, _nonEmptyStringValidator,
                ConfigurationPropertyOptions.IsKey | ConfigurationPropertyOptions.IsRequired);

            _propUrl = new ConfigurationProperty(
                "url", typeof (string), null, null,
                _nonEmptyStringValidator,
                ConfigurationPropertyOptions.IsRequired);

            _propHandlerType = new ConfigurationProperty(
                "routeHandlerType", typeof (string),
                string.Empty, ConfigurationPropertyOptions.None);

            _propDefaults = new ConfigurationProperty(
                "defaults", typeof (RouteAttributeElement),
                null, ConfigurationPropertyOptions.None);

            _propConstraints = new ConfigurationProperty(
                "constraints", typeof (RouteAttributeElement),
                null, ConfigurationPropertyOptions.None);

            _propDataTokens = new ConfigurationProperty(
                "dataTokens", typeof (RouteAttributeElement),
                null, ConfigurationPropertyOptions.None);

            _properties = new ConfigurationPropertyCollection();
            _properties.Add(_propName);
            _properties.Add(_propUrl);
            _properties.Add(_propHandlerType);
            _properties.Add(_propDefaults);
            _properties.Add(_propConstraints);
            _properties.Add(_propDataTokens);
        }
 public ConfigurationValidatorWrappingValidator(ConfigurationValidatorBase validatorInstance, object convertedValue)
 {
     this.validatorInstance = validatorInstance;
     this.convertedValue = convertedValue;
 }
 private void ConstructorInit(string name, System.Type type, ConfigurationPropertyOptions options, ConfigurationValidatorBase validator, TypeConverter converter)
 {
     if (typeof(ConfigurationSection).IsAssignableFrom(type))
     {
         throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_properties_may_not_be_derived_from_configuration_section", new object[] { name }));
     }
     this._providedName = name;
     if (((options & ConfigurationPropertyOptions.IsDefaultCollection) != ConfigurationPropertyOptions.None) && string.IsNullOrEmpty(name))
     {
         name = DefaultCollectionPropertyName;
     }
     else
     {
         this.ValidatePropertyName(name);
     }
     this._name = name;
     this._type = type;
     this._options = options;
     this._validator = validator;
     this._converter = converter;
     if (this._validator == null)
     {
         this._validator = DefaultValidatorInstance;
     }
     else if (!this._validator.CanValidate(this._type))
     {
         throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Validator_does_not_support_prop_type", new object[] { this._name }));
     }
 }
 public ConfigurationProperty(string name, Type type, Object defaultValue, System.ComponentModel.TypeConverter typeConverter, ConfigurationValidatorBase validator, ConfigurationPropertyOptions options, string description)
 {
 }
 public ConfigurationElementProperty(ConfigurationValidatorBase validator)
 {
     Contract.Requires(validator != null);
 }
 public ConfigurationElementProperty(ConfigurationValidatorBase validator)
 {
 }
예제 #22
0
        internal ConfigurationProperty(PropertyInfo info)
        {
            Debug.Assert(info != null, "info != null");

            ConfigurationPropertyAttribute propertyAttribute    = null;
            DescriptionAttribute           descriptionAttribute = null;

            // For compatibility we read the component model default value attribute. It is only
            // used if ConfigurationPropertyAttribute doesn't provide the default value.
            DefaultValueAttribute defaultValueAttribute = null;

            TypeConverter typeConverter          = null;
            ConfigurationValidatorBase validator = null;

            // Look for relevant attributes
            foreach (Attribute attribute in Attribute.GetCustomAttributes(info))
            {
                if (attribute is TypeConverterAttribute)
                {
                    typeConverter = TypeUtil.CreateInstance <TypeConverter>(((TypeConverterAttribute)attribute).ConverterTypeName);
                }
                else if (attribute is ConfigurationPropertyAttribute)
                {
                    propertyAttribute = (ConfigurationPropertyAttribute)attribute;
                }
                else if (attribute is ConfigurationValidatorAttribute)
                {
                    if (validator != null)
                    {
                        // We only allow one validator to be specified on a property.
                        //
                        // Consider: introduce a new validator type ( CompositeValidator ) that is a
                        // list of validators and executes them all

                        throw new ConfigurationErrorsException(
                                  SR.Format(SR.Validator_multiple_validator_attributes, info.Name));
                    }

                    ConfigurationValidatorAttribute validatorAttribute = (ConfigurationValidatorAttribute)attribute;
                    validatorAttribute.SetDeclaringType(info.DeclaringType);
                    validator = validatorAttribute.ValidatorInstance;
                }
                else if (attribute is DescriptionAttribute)
                {
                    descriptionAttribute = (DescriptionAttribute)attribute;
                }
                else if (attribute is DefaultValueAttribute)
                {
                    defaultValueAttribute = (DefaultValueAttribute)attribute;
                }
            }

            Type propertyType = info.PropertyType;

            // If the property is a Collection we need to look for the ConfigurationCollectionAttribute for
            // additional customization.
            if (typeof(ConfigurationElementCollection).IsAssignableFrom(propertyType))
            {
                // Look for the ConfigurationCollection attribute on the property itself, fall back
                // on the property type.
                ConfigurationCollectionAttribute collectionAttribute =
                    Attribute.GetCustomAttribute(info,
                                                 typeof(ConfigurationCollectionAttribute)) as ConfigurationCollectionAttribute ??
                    Attribute.GetCustomAttribute(propertyType,
                                                 typeof(ConfigurationCollectionAttribute)) as ConfigurationCollectionAttribute;

                if (collectionAttribute != null)
                {
                    if (collectionAttribute.AddItemName.IndexOf(',') == -1)
                    {
                        AddElementName = collectionAttribute.AddItemName;                                                     // string.Contains(char) is .NetCore2.1+ specific
                    }
                    RemoveElementName = collectionAttribute.RemoveItemName;
                    ClearElementName  = collectionAttribute.ClearItemsName;
                }
            }

            // This constructor shouldn't be invoked if the reflection info is not for an actual config property
            Debug.Assert(propertyAttribute != null, "attribProperty != null");

            ConstructorInit(propertyAttribute.Name,
                            info.PropertyType,
                            propertyAttribute.Options,
                            validator,
                            typeConverter);

            // Figure out the default value
            InitDefaultValueFromTypeInfo(propertyAttribute, defaultValueAttribute);

            // Get the description
            if (!string.IsNullOrEmpty(descriptionAttribute?.Description))
            {
                Description = descriptionAttribute.Description;
            }
        }
 protected internal virtual ConfigurationProperty CreateNameConfigurationProperty(object defaultValue, ConfigurationPropertyOptions options, ConfigurationValidatorBase validator)
 {
     return new ConfigurationProperty(this.NamePropertyName, typeof(string), defaultValue, null, validator, options);
 }
 public ConfigurationElementProperty(ConfigurationValidatorBase validator)
 {
   Contract.Requires(validator != null);
 }
예제 #25
0
 internal static void ValidateElement(ConfigurationElement elem, ConfigurationValidatorBase propValidator, bool recursive)
 {
     ConfigurationValidatorBase configurationValidatorBase = propValidator;
       if (configurationValidatorBase == null)
       {
     if (elem.ElementProperty != null)
     {
       configurationValidatorBase = elem.ElementProperty.Validator;
       if (configurationValidatorBase != null)
       {
     if (!configurationValidatorBase.CanValidate(elem.GetType()))
       throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Validator_does_not_support_elem_type", new object[1]
       {
         (object) elem.GetType().Name
       }));
       }
     }
       }
       try
       {
     if (configurationValidatorBase != null)
       configurationValidatorBase.Validate((object) elem);
       }
       catch (ConfigurationException ex)
       {
     throw;
       }
       catch (Exception ex)
       {
     throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Validator_element_not_valid", (object) elem._elementTagName, (object) ex.Message));
       }
       if (!recursive)
     return;
       if (elem is ConfigurationElementCollection && elem is ConfigurationElementCollection)
       {
     IEnumerator elementsEnumerator = ((ConfigurationElementCollection) elem).GetElementsEnumerator();
     while (elementsEnumerator.MoveNext())
       ConfigurationElement.ValidateElement((ConfigurationElement) elementsEnumerator.Current, (ConfigurationValidatorBase) null, true);
       }
       for (int index = 0; index < elem.Values.Count; ++index)
       {
     ConfigurationElement elem1 = elem.Values[index] as ConfigurationElement;
     if (elem1 != null)
       ConfigurationElement.ValidateElement(elem1, (ConfigurationValidatorBase) null, true);
       }
 }
        private static void CachePerTypeValidator( Type type, ConfigurationValidatorBase validator ) {
            Debug.Assert((type != null) && ( validator != null));
            Debug.Assert(typeof(ConfigurationElement).IsAssignableFrom(type));

            // Use the same lock as the property bag lock since in the current implementation
            // the only way to get to this method is through the code path that locks the property bag cache first ( see PropertiesFromType() )

            // NOTE[ Thread Safety ]: Non-guarded access to static variable - since this code is called only from CreatePropertyBagFromType
            // which in turn is done onle once per type and is guarded by the s_propertyBag.SyncRoot then this call is thread safe as well
            if (s_perTypeValidators == null ) {
                    s_perTypeValidators = new Dictionary<Type,ConfigurationValidatorBase>();
            }

            // A type validator should be cached only once. If it isn't then attribute parsing is done more then once which should be avoided
            Debug.Assert( !s_perTypeValidators.ContainsKey(type));

            // Make sure the supplied validator supports validating this object
            if (!validator.CanValidate(type)) {
                throw new ConfigurationErrorsException(SR.GetString(SR.Validator_does_not_support_elem_type, 
                                                       type.Name));
            }

            s_perTypeValidators.Add(type, validator);
        }
        private void ConstructorInit(string name,
                                        Type type,
                                        ConfigurationPropertyOptions options,
                                        ConfigurationValidatorBase validator,
                                        TypeConverter converter) {
            if (typeof(ConfigurationSection).IsAssignableFrom(type)) {
                throw new ConfigurationErrorsException(SR.GetString(SR.Config_properties_may_not_be_derived_from_configuration_section, name));
            }

            _providedName = name; // save the provided name so we can check for default collection names
            if ((options & ConfigurationPropertyOptions.IsDefaultCollection) != 0 &&
                String.IsNullOrEmpty(name)) {
                name = DefaultCollectionPropertyName;
            }
            else {
                ValidatePropertyName(name);
            }

            _name = name;
            _type = type;
            _options = options;
            _validator = validator;
            _converter = converter;

            // Use the default validator if none was supplied
            if (_validator == null) {
                _validator = DefaultValidatorInstance;
            }
            else {
                // Make sure the supplied validator supports the type of this property
                if (!_validator.CanValidate(_type)) {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Validator_does_not_support_prop_type, _name));
                }
            }
        }
 public ConfigurationValidatorWrappingValidator(ConfigurationValidatorBase validatorInstance)
 {
     this.validatorInstance = validatorInstance;
 }
 internal static void ValidateElement(ConfigurationElement elem, ConfigurationValidatorBase propValidator, bool recursive)
 {
     ConfigurationValidatorBase validator = propValidator;
     if ((validator == null) && (elem.ElementProperty != null))
     {
         validator = elem.ElementProperty.Validator;
         if ((validator != null) && !validator.CanValidate(elem.GetType()))
         {
             throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Validator_does_not_support_elem_type", new object[] { elem.GetType().Name }));
         }
     }
     try
     {
         if (validator != null)
         {
             validator.Validate(elem);
         }
     }
     catch (ConfigurationException)
     {
         throw;
     }
     catch (Exception exception)
     {
         throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Validator_element_not_valid", new object[] { elem._elementTagName, exception.Message }));
     }
     if (recursive)
     {
         if ((elem is ConfigurationElementCollection) && (elem is ConfigurationElementCollection))
         {
             IEnumerator elementsEnumerator = ((ConfigurationElementCollection) elem).GetElementsEnumerator();
             while (elementsEnumerator.MoveNext())
             {
                 ValidateElement((ConfigurationElement) elementsEnumerator.Current, null, true);
             }
         }
         for (int i = 0; i < elem.Values.Count; i++)
         {
             ConfigurationElement element = elem.Values[i] as ConfigurationElement;
             if (element != null)
             {
                 ValidateElement(element, null, true);
             }
         }
     }
 }
        internal static void ValidateElement(ConfigurationElement elem, ConfigurationValidatorBase propValidator, bool recursive) {
            // Validate a config element with the per-type validator when a per-property ( propValidator ) is not supplied
            // or with the per-prop validator when the element ( elem ) is a child property of another configuration element

            ConfigurationValidatorBase validator = propValidator;

            if ((validator == null) &&   // Not a property - use the per-type validator
                (elem.ElementProperty != null)) {
                validator = elem.ElementProperty.Validator;

                // Since ElementProperty can be overriden by derived classes we need to make sure that
                // the validator supports the type of elem every time
                if ((validator != null) && !validator.CanValidate(elem.GetType())) {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Validator_does_not_support_elem_type, elem.GetType().Name));
                }
            }

            try {
                if (validator != null) {
                    validator.Validate(elem);
                }
            }
            catch (ConfigurationException) {
                // ConfigurationElement validators are allowed to throw ConfigurationErrorsException. 
                throw;
            }
            catch (Exception ex) {
                throw new ConfigurationErrorsException(SR.GetString(SR.Validator_element_not_valid, elem._elementTagName, ex.Message));
            }
            catch {
                throw new ConfigurationErrorsException(SR.GetString(SR.Validator_element_not_valid, elem._elementTagName, ExceptionUtil.NoExceptionInformation));
            }

            if (recursive == true) {
                if (elem is ConfigurationElementCollection) {
                    if (elem is ConfigurationElementCollection) {
                        IEnumerator it = ((ConfigurationElementCollection)elem).GetElementsEnumerator();
                        while( it.MoveNext() ) {
                            ValidateElement((ConfigurationElement)it.Current, null, true);
                        }
                    }   
                }
                
                // Validate all child elements recursively
                for (int index = 0; index < elem.Values.Count; index++) {
                    ConfigurationElement value = elem.Values[index] as ConfigurationElement;

                    if (value != null) {
                        // Run the per-type validator on the child element and proceed with validation in subelements
                        // Note we dont run the per-property validator here since we run those when the property value is set
                        ValidateElement(value, null, true);              // per-type
                    }
                }
            }
        }
        internal ConfigurationProperty(PropertyInfo info)
        {
            Debug.Assert(info != null, "info != null");

            // Bellow are the attributes we handle
            TypeConverterAttribute          attribConverter = null;
            ConfigurationPropertyAttribute  attribProperty  = null;
            ConfigurationValidatorAttribute attribValidator = null;

            // Compatability attributes
            // If the approprite data is provided in the ConfigPropAttribute then the one bellow will be ignored
            DescriptionAttribute  attribStdDescription = null;
            DefaultValueAttribute attribStdDefault     = null;

            TypeConverter typeConverter          = null;
            ConfigurationValidatorBase validator = null;

            // Find the interesting attributes in the collection
            foreach (Attribute attribute in Attribute.GetCustomAttributes(info))
            {
                if (attribute is TypeConverterAttribute)
                {
                    attribConverter = (TypeConverterAttribute)attribute;
                    typeConverter   = (TypeConverter)TypeUtil.CreateInstanceWithReflectionPermission(attribConverter.ConverterTypeName);
                }
                else if (attribute is ConfigurationPropertyAttribute)
                {
                    attribProperty = (ConfigurationPropertyAttribute)attribute;
                }
                else if (attribute is ConfigurationValidatorAttribute)
                {
                    if (validator != null)
                    {
                        throw new ConfigurationErrorsException(SR.GetString(SR.Validator_multiple_validator_attributes, info.Name));
                    }

                    attribValidator = (ConfigurationValidatorAttribute)attribute;
                    validator       = attribValidator.ValidatorInstance;
                }
                else if (attribute is DescriptionAttribute)
                {
                    attribStdDescription = (DescriptionAttribute)attribute;
                }
                else if (attribute is DefaultValueAttribute)
                {
                    attribStdDefault = (DefaultValueAttribute)attribute;
                }
            }

            Type propertyType = info.PropertyType;

            // Collections need some customization when the collection attribute is present
            if (typeof(ConfigurationElementCollection).IsAssignableFrom(propertyType))
            {
                ConfigurationCollectionAttribute attribCollection =
                    Attribute.GetCustomAttribute(info,
                                                 typeof(ConfigurationCollectionAttribute)) as ConfigurationCollectionAttribute;

                // If none on the property - see if there is an attribute on the collection type itself
                if (attribCollection == null)
                {
                    attribCollection =
                        Attribute.GetCustomAttribute(propertyType,
                                                     typeof(ConfigurationCollectionAttribute)) as ConfigurationCollectionAttribute;
                }
                if (attribCollection != null)
                {
                    if (attribCollection.AddItemName.IndexOf(',') == -1)
                    {
                        _addElementName = attribCollection.AddItemName;
                    }
                    _removeElementName = attribCollection.RemoveItemName;
                    _clearElementName  = attribCollection.ClearItemsName;
                }
            }

            // This constructor shouldnt be invoked if the reflection info is not for an actual config property
            Debug.Assert(attribProperty != null, "attribProperty != null");

            ConstructorInit(attribProperty.Name,
                            info.PropertyType,
                            attribProperty.Options,
                            validator,
                            typeConverter);

            // Figure out the default value
            InitDefaultValueFromTypeInfo(attribProperty, attribStdDefault);

            // Get the description
            if ((attribStdDescription != null) && !string.IsNullOrEmpty(attribStdDescription.Description))
            {
                _description = attribStdDescription.Description;
            }
        }
 public ConfigurationElementProperty(ConfigurationValidatorBase validator)
 {
 }
예제 #33
0
        internal ConfigurationProperty(PropertyInfo info)
        {
            Debug.Assert(info != null, "info != null");

            // Bellow are the attributes we handle
            ConfigurationPropertyAttribute attribProperty = null;

            // Compatability attributes
            // If the approprite data is provided in the ConfigPropAttribute then the one bellow will be ignored
            DescriptionAttribute  attribStdDescription = null;
            DefaultValueAttribute attribStdDefault     = null;

            TypeConverter typeConverter          = null;
            ConfigurationValidatorBase validator = null;

            // Find the interesting attributes in the collection
            foreach (Attribute attribute in Attribute.GetCustomAttributes(info))
            {
                if (attribute is TypeConverterAttribute)
                {
                    TypeConverterAttribute attribConverter = (TypeConverterAttribute)attribute;
                    typeConverter = TypeUtil.CreateInstance <TypeConverter>(attribConverter.ConverterTypeName);
                }
                else
                {
                    if (attribute is ConfigurationPropertyAttribute)
                    {
                        attribProperty = (ConfigurationPropertyAttribute)attribute;
                    }
                    else
                    {
                        if (attribute is ConfigurationValidatorAttribute)
                        {
                            // There could be more then one validator attribute specified on a property
                            // Currently we consider this an error since it's too late to fix it for whidbey
                            // but the right thing to do is to introduce new validator type ( CompositeValidator ) that is a list of validators and executes
                            // them all

                            if (validator != null)
                            {
                                throw new ConfigurationErrorsException(
                                          string.Format(SR.Validator_multiple_validator_attributes, info.Name));
                            }

                            ConfigurationValidatorAttribute attribValidator = (ConfigurationValidatorAttribute)attribute;
                            attribValidator.SetDeclaringType(info.DeclaringType);
                            validator = attribValidator.ValidatorInstance;
                        }
                        else
                        {
                            if (attribute is DescriptionAttribute)
                            {
                                attribStdDescription = (DescriptionAttribute)attribute;
                            }
                            else
                            {
                                if (attribute is DefaultValueAttribute)
                                {
                                    attribStdDefault = (DefaultValueAttribute)attribute;
                                }
                            }
                        }
                    }
                }
            }

            Type propertyType = info.PropertyType;

            // Collections need some customization when the collection attribute is present
            if (typeof(ConfigurationElementCollection).IsAssignableFrom(propertyType))
            {
                ConfigurationCollectionAttribute attribCollection =
                    Attribute.GetCustomAttribute(info,
                                                 typeof(ConfigurationCollectionAttribute)) as ConfigurationCollectionAttribute ??
                    Attribute.GetCustomAttribute(propertyType,
                                                 typeof(ConfigurationCollectionAttribute)) as ConfigurationCollectionAttribute;

                // If none on the property - see if there is an attribute on the collection type itself
                if (attribCollection != null)
                {
                    if (attribCollection.AddItemName.IndexOf(',') == -1)
                    {
                        AddElementName = attribCollection.AddItemName;
                    }
                    RemoveElementName = attribCollection.RemoveItemName;
                    ClearElementName  = attribCollection.ClearItemsName;
                }
            }

            // This constructor shouldnt be invoked if the reflection info is not for an actual config property
            Debug.Assert(attribProperty != null, "attribProperty != null");

            ConstructorInit(attribProperty.Name,
                            info.PropertyType,
                            attribProperty.Options,
                            validator,
                            typeConverter);

            // Figure out the default value
            InitDefaultValueFromTypeInfo(attribProperty, attribStdDefault);

            // Get the description
            if (!string.IsNullOrEmpty(attribStdDescription?.Description))
            {
                Description = attribStdDescription.Description;
            }
        }
 private static void CachePerTypeValidator(Type type, ConfigurationValidatorBase validator)
 {
     if (s_perTypeValidators == null)
     {
         s_perTypeValidators = new Dictionary<Type, ConfigurationValidatorBase>();
     }
     if (!validator.CanValidate(type))
     {
         throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Validator_does_not_support_elem_type", new object[] { type.Name }));
     }
     s_perTypeValidators.Add(type, validator);
 }
예제 #35
0
        internal ConfigurationProperty(PropertyInfo info)
        {
            TypeConverterAttribute          attribute        = null;
            ConfigurationPropertyAttribute  attribProperty   = null;
            ConfigurationValidatorAttribute attribute3       = null;
            DescriptionAttribute            attribute4       = null;
            DefaultValueAttribute           attribStdDefault = null;
            TypeConverter converter = null;
            ConfigurationValidatorBase validator = null;

            foreach (Attribute attribute6 in Attribute.GetCustomAttributes(info))
            {
                if (attribute6 is TypeConverterAttribute)
                {
                    attribute = (TypeConverterAttribute)attribute6;
                    converter = (TypeConverter)System.Configuration.TypeUtil.CreateInstanceWithReflectionPermission(attribute.ConverterTypeName);
                }
                else if (attribute6 is ConfigurationPropertyAttribute)
                {
                    attribProperty = (ConfigurationPropertyAttribute)attribute6;
                }
                else if (attribute6 is ConfigurationValidatorAttribute)
                {
                    if (validator != null)
                    {
                        throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Validator_multiple_validator_attributes", new object[] { info.Name }));
                    }
                    attribute3 = (ConfigurationValidatorAttribute)attribute6;
                    validator  = attribute3.ValidatorInstance;
                }
                else if (attribute6 is DescriptionAttribute)
                {
                    attribute4 = (DescriptionAttribute)attribute6;
                }
                else if (attribute6 is DefaultValueAttribute)
                {
                    attribStdDefault = (DefaultValueAttribute)attribute6;
                }
            }
            System.Type propertyType = info.PropertyType;
            if (typeof(ConfigurationElementCollection).IsAssignableFrom(propertyType))
            {
                ConfigurationCollectionAttribute customAttribute = Attribute.GetCustomAttribute(info, typeof(ConfigurationCollectionAttribute)) as ConfigurationCollectionAttribute;
                if (customAttribute == null)
                {
                    customAttribute = Attribute.GetCustomAttribute(propertyType, typeof(ConfigurationCollectionAttribute)) as ConfigurationCollectionAttribute;
                }
                if (customAttribute != null)
                {
                    if (customAttribute.AddItemName.IndexOf(',') == -1)
                    {
                        this._addElementName = customAttribute.AddItemName;
                    }
                    this._removeElementName = customAttribute.RemoveItemName;
                    this._clearElementName  = customAttribute.ClearItemsName;
                }
            }
            this.ConstructorInit(attribProperty.Name, info.PropertyType, attribProperty.Options, validator, converter);
            this.InitDefaultValueFromTypeInfo(attribProperty, attribStdDefault);
            if ((attribute4 != null) && !string.IsNullOrEmpty(attribute4.Description))
            {
                this._description = attribute4.Description;
            }
        }
예제 #36
0
        internal static void ValidateElement(ConfigurationElement elem, ConfigurationValidatorBase propValidator,
            bool recursive)
        {
            // Validate a config element with the per-type validator when a per-property ( propValidator ) is not supplied
            // or with the per-prop validator when the element ( elem ) is a child property of another configuration element

            ConfigurationValidatorBase validator = propValidator;

            if ((validator == null) && // Not a property - use the per-type validator
                (elem.ElementProperty != null))
            {
                validator = elem.ElementProperty.Validator;

                // Since ElementProperty can be overriden by derived classes we need to make sure that
                // the validator supports the type of elem every time
                if ((validator != null) && !validator.CanValidate(elem.GetType()))
                {
                    throw new ConfigurationErrorsException(string.Format(SR.Validator_does_not_support_elem_type,
                        elem.GetType().Name));
                }
            }

            try
            {
                validator?.Validate(elem);
            }
            catch (ConfigurationException)
            {
                // ConfigurationElement validators are allowed to throw ConfigurationErrorsException.
                throw;
            }
            catch (Exception ex)
            {
                throw new ConfigurationErrorsException(string.Format(SR.Validator_element_not_valid, elem.ElementTagName,
                    ex.Message));
            }

            if (recursive)
            {
                // Validate collection items:
                // Note: this is a bit of a hack - we will exploit the fact that this method is called with recursive == true only when serializing the top level section
                // At deserializtion time the per-element validator for collection items will get executed as part of their deserialization logic
                // However we dont perform validation in the serialization logic ( because at that time the object is unmerged and not all data is present )
                // so we have to do that validation here.
                if (elem is ConfigurationElementCollection)
                {
                    IEnumerator it = ((ConfigurationElementCollection)elem).GetElementsEnumerator();
                    while (it.MoveNext()) ValidateElement((ConfigurationElement)it.Current, null, true);
                }

                // Validate all child elements recursively
                for (int index = 0; index < elem.Values.Count; index++)
                {
                    ConfigurationElement value = elem.Values[index] as ConfigurationElement;

                    if (value != null)
                    {
                        // Run the per-type validator on the child element and proceed with validation in subelements
                        // Note we dont run the per-property validator here since we run those when the property value is set
                        ValidateElement(value, null, true); // per-type
                    }
                }
            }
        }
 public ConfigurationProperty(string name, Type type, Object defaultValue, System.ComponentModel.TypeConverter typeConverter, ConfigurationValidatorBase validator, ConfigurationPropertyOptions options, string description)
 {
 }