示例#1
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);
            }
        }
 public IntegerValidatorParser(PropertyInfo property, ConfigurationValidatorAttribute attribute)
     : base(property, attribute)
 {
 }
示例#3
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;
            }
        }
 /// <summary>
 /// </summary>
 protected ValidatorAttributeParser(PropertyInfo property, ConfigurationValidatorAttribute attribute)
 {
     Property = property;
     Attribute = attribute;
 }
示例#5
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;
            }
        }
        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;
            }
        }
 /// <summary>
 /// </summary>
 public PositiveTimeSpanValidatorParser(PropertyInfo property, ConfigurationValidatorAttribute attribute)
     : base(property, attribute)
 {
 }
示例#8
0
 /// <summary>
 /// </summary>
 protected NoValidatorParser(PropertyInfo property, ConfigurationValidatorAttribute attribute)
     : base(property, attribute)
 {
 }
        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;
            }
        }