Пример #1
0
 public static KeyInfo Create(ConfigurationPropertyAttribute prop)
 {
     return new KeyInfo
     {
         Name = prop.Name,
         IsRequired = prop.IsRequired,
         DefaultValue = prop.DefaultValue
     };
 }
Пример #2
0
        private static bool IsDefaultValue(object propertyValue, ConfigurationPropertyAttribute configurationPropertyAttribute)
        {
            var type = propertyValue.GetType();

            if (type == typeof(IPAddress) || type == typeof(IPEndPoint))
                return propertyValue.ToString().Equals(configurationPropertyAttribute.DefaultValue.ToString());

            return propertyValue.Equals(configurationPropertyAttribute.DefaultValue);
        }
Пример #3
0
        public static void AddAnnotation(this XmlSchemaAnnotated annotatedType, Type type, ConfigurationPropertyAttribute configProperty)
        {
            annotatedType.Annotation = new XmlSchemaAnnotation();

            //  human documentation
            var descriptionAtts = TypeParser.GetAttributes<DescriptionAttribute>(type);
            var xmlDocumentation = type.GetXmlDocumentation();
            var typeName = type.FullName;

            ApplyAnnotation(annotatedType, descriptionAtts, configProperty, xmlDocumentation, typeName, typeName);
        }
Пример #4
0
        /// <summary>
        ///     Provides standard documentation for a type in the form of XmlSchemaDocumentation objects
        /// </summary>
        public static void AddAnnotation(this XmlSchemaAnnotated annotatedType, PropertyInfo property, ConfigurationPropertyAttribute configProperty)
        {
            annotatedType.Annotation = new XmlSchemaAnnotation();

            //  human documentation
            var descriptionAtts = TypeParser.GetAttributes<DescriptionAttribute>(property);
            var xmlDocumentation = property.GetXmlDocumentation();
            var fullName = property.PropertyType.FullName;
            var typeName = String.Format("{0}{1}", property.DeclaringType.FullName, property.Name);

            ApplyAnnotation(annotatedType, descriptionAtts, configProperty, xmlDocumentation, typeName, fullName);
        }
Пример #5
0
        public PropertyViewModel(SectionViewModel parent, PropertyInfo property, ConfigurationPropertyAttribute attribute)
        {
            if (parent == null)
                throw new ArgumentNullException("parent");
            if (property == null)
                throw new ArgumentNullException("property");
            if (attribute == null)
                throw new ArgumentNullException("attribute");

            Parent = parent;

            Property = property;
            Attribute = attribute;

            DefaultValue = Attribute.DefaultValue;
            Value = DefaultValue;
        }
        ///<summary>
        /// Initializes an instance of ElementProperty.
        ///</summary>
        ///<param name="serviceProvider">Service provider used to locate certain services for the configuration system.</param>
        ///<param name="parent">The parent <see cref="ElementViewModel"/> owning the property.</param>
        ///<param name="declaringProperty">The description of the property.</param>
        ///<param name="additionalAttributes">Additional attributes made available to the ElementProperty.</param>
        ///<exception cref="ArgumentNullException"></exception>
        public ElementProperty(IServiceProvider serviceProvider, ElementViewModel parent, PropertyDescriptor declaringProperty, IEnumerable<Attribute> additionalAttributes)
            : base(serviceProvider, parent == null ? null : parent.ConfigurationElement, declaringProperty, additionalAttributes)
        {
            if (parent == null) throw new ArgumentNullException("parent");
            declaringElement = parent;

            ConfigurationElement parentElement = parent.ConfigurationElement;

            configurationPropertyAttribute = declaringProperty.Attributes.OfType<ConfigurationPropertyAttribute>().FirstOrDefault();
            if (configurationPropertyAttribute == null)
            {
                configurationPropertyAttribute = additionalAttributes.OfType<ConfigurationPropertyAttribute>().FirstOrDefault();
            }
            if (configurationPropertyAttribute != null)
            {
                configurationProperty = parentElement.ElementInformation.Properties[configurationPropertyAttribute.Name];
            }

            this.declaringElement.PropertyChanged += DeclaringElementPropertyChanged;
        }
 private void InitDefaultValueFromTypeInfo(ConfigurationPropertyAttribute attribProperty, DefaultValueAttribute attribStdDefault)
 {
     object defaultValue = attribProperty.DefaultValue;
     if (((defaultValue == null) || (defaultValue == ConfigurationElement.s_nullPropertyValue)) && (attribStdDefault != null))
     {
         defaultValue = attribStdDefault.Value;
     }
     if (((defaultValue != null) && (defaultValue is string)) && (this._type != typeof(string)))
     {
         try
         {
             defaultValue = this.Converter.ConvertFromInvariantString((string) defaultValue);
         }
         catch (Exception exception)
         {
             throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Default_value_conversion_error_from_string", new object[] { this._name, exception.Message }));
         }
     }
     if ((defaultValue == null) || (defaultValue == ConfigurationElement.s_nullPropertyValue))
     {
         if (this._type == typeof(string))
         {
             defaultValue = string.Empty;
         }
         else if (this._type.IsValueType)
         {
             defaultValue = System.Configuration.TypeUtil.CreateInstanceWithReflectionPermission(this._type);
         }
     }
     this.SetDefaultValue(defaultValue);
 }
Пример #8
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,
                            descriptionAttribute?.Description);

            // Figure out the default value
            InitDefaultValueFromTypeInfo(propertyAttribute, defaultValueAttribute);
        }
Пример #9
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;
            }
        }
        private void InitDefaultValueFromTypeInfo(ConfigurationPropertyAttribute attribProperty,
                                                    DefaultValueAttribute attribStdDefault) {
            object defaultValue = attribProperty.DefaultValue;

            // If there is no default value there - try the other attribute ( the clr standard one )
            if ((defaultValue == null || defaultValue == ConfigurationElement.s_nullPropertyValue) &&
                (attribStdDefault != null)) {
                defaultValue = attribStdDefault.Value;
            }

            // If there was a default value in the prop attribute - check if we need to convert it from string
            if ((defaultValue != null) && (defaultValue is string) && (_type != typeof(string))) {
                // Use the converter to parse this property default value
                try {
                    defaultValue = Converter.ConvertFromInvariantString((string)defaultValue);
                }
                catch (Exception ex) {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Default_value_conversion_error_from_string, _name, ex.Message));
                }
            }
            if (defaultValue == null || defaultValue == ConfigurationElement.s_nullPropertyValue) {
                if (_type == typeof(string)) {
                    defaultValue = String.Empty;
                }
                else if (_type.IsValueType) {
                    defaultValue = TypeUtil.CreateInstanceWithReflectionPermission(_type);
                }
            }
            SetDefaultValue(defaultValue);
        }
Пример #11
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;
            }
        }
Пример #12
0
        private static void ApplyAnnotation(XmlSchemaAnnotated annotatedType, DescriptionAttribute[] descriptionAtts,
            ConfigurationPropertyAttribute configProperty, string xmlDocumentation, string typeName, string fullName)
        {
            string standardDesc;

            if (configProperty != null)
            {
                standardDesc = configProperty.IsRequired ? "Required" : "Optional";
                standardDesc += " " + fullName;
                standardDesc += " " +
                                (configProperty.DefaultValue == null || configProperty.DefaultValue.ToString() == "System.Object"
                                    ? string.Empty
                                    : "[" + configProperty.DefaultValue + "]");
            }
            else
            {
                standardDesc = string.Empty;
            }

            var documentation = new XmlSchemaDocumentation();
            if (descriptionAtts.Length > 0)
            {
                documentation.Markup = TextToNodeArray(descriptionAtts[0].Description + " " + standardDesc);
            }
            else if (!String.IsNullOrEmpty(xmlDocumentation))
            {
                // normalise line endings and remove trailing whitespace(s)
                xmlDocumentation = Regex.Replace(xmlDocumentation, @"\s*(\r\n|\n\r|\n|\r)", "\r\n");

                documentation.Markup = TextToNodeArray(xmlDocumentation);
            }
            else
            {
                documentation.Markup = TextToNodeArray(standardDesc);
            }

            //  machine documentation
            var appInfo = new XmlSchemaAppInfo
            {
                Markup = TextToNodeArray(typeName)
            };

            //  add the documentation to the object
            annotatedType.Annotation.Items.Add(documentation);
            annotatedType.Annotation.Items.Add(appInfo);
        }
        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;
            }
        }