/// <summary> /// Initializes a new instance of <see cref="CollectionElementViewModel"/>. /// </summary> /// <param name="containingCollection">The collection containing this element.</param> /// <param name="thisElement">The <see cref="ConfigurationElement"/> modeled by the <see cref="CollectionElementViewModel"/>.</param> /// <param name="additionalAttributes">Additional <see cref="Attribute"/> items to apply when describing this <paramref name="thisElement"/>.</param> public CollectionElementViewModel(ElementCollectionViewModel containingCollection, ConfigurationElement thisElement, IEnumerable<Attribute> additionalAttributes) :base(containingCollection, thisElement, additionalAttributes) { this.containingCollection = containingCollection; configurationCollectionAttribute = containingCollection.Attributes.OfType<ConfigurationCollectionAttribute>().First(); var overrides = new Dictionary<Type, object>() { {typeof (ElementCollectionViewModel), containingCollection}, {typeof (CollectionElementViewModel), this} }; MoveUp = ContainingSection.CreateCommand<MoveUpCommand>(overrides); MoveDown = ContainingSection.CreateCommand<MoveDownCommand>(overrides); }
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); }
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; } }
/// <summary> /// Adds the child elements of the collection /// </summary> protected void AddCollectionChildren(XmlSchemaGroupBase parentParticle, ConfigurationCollectionAttribute configCollAttribute, int level) { Debug.IndentLevel = level; XmlSchemaComplexType ct; if (Generator.ComplexMap.ContainsKey(configCollAttribute.ItemType)) { //already done the work ct = Generator.ComplexMap[configCollAttribute.ItemType]; } else { // got to generate a new one for the collection ct = new XmlSchemaComplexType { Name = configCollAttribute.ItemType.Name + "CT" }; ct.CreateSchemaSequenceParticle(); Generator.ComplexMap.Add(configCollAttribute.ItemType, ct); Generator.Schema.Items.Add(ct); } // add method var addElement = new XmlSchemaElement { Name = configCollAttribute.AddItemName, MinOccurs = 0, SchemaTypeName = new XmlQualifiedName(XmlHelper.PrependNamespaceAlias(ct.Name)) }; // ok we now have the child element as a complextype object // but we need to add three elements to the parent complex type // which support the add / remove / clear methods of the collection // if we must add a clear method then it has to come first // the basic map types do not include the clear and remove methods if (configCollAttribute.CollectionType == ConfigurationElementCollectionType.AddRemoveClearMap || configCollAttribute.CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate) { // clear method (which is a simple element) var element = new XmlSchemaElement { Name = configCollAttribute.ClearItemsName, MinOccurs = 0, MaxOccurs = 1, //SchemaType = new XmlSchemaComplexType() }; parentParticle.Items.Add(element); // remove method element = new XmlSchemaElement { MinOccurs = 0, Name = configCollAttribute.RemoveItemName }; var removeCt = new XmlSchemaComplexType(); element.SchemaType = removeCt; // get the type contained in the collection and work out // what the key property is var childElementType = configCollAttribute.ItemType; var found = false; foreach (var pi in childElementType.GetProperties()) { if (found) break; Debug.WriteLine("Child property: " + pi.Name); var cpAtts = GetAttributes<ConfigurationPropertyAttribute>(pi); // this should return a standardtypeparser object, if it doesnt // then it means that the key of the element was an element // itself and I don't think that is possible! if (cpAtts.Any(att => att.IsKey)) { var parser = TypeParserFactory.GetParser(Generator, pi); parser.GenerateSchemaTypeObjects(pi, removeCt, level + 1); found = true; } } parentParticle.Items.Add(element); SetMaxOccurs(element, parentParticle); } SetMaxOccurs(addElement, parentParticle); parentParticle.Items.Add(addElement); // get all properties from the configuration object var propertyInfos = GetProperties<ConfigurationPropertyAttribute>(configCollAttribute.ItemType); foreach (var pi in propertyInfos) { var parser = TypeParserFactory.GetParser(Generator, pi); Debug.WriteLine("\tConfigurationProperty: {0} {1}", pi.Name, parser.GetType()); parser.GenerateSchemaTypeObjects(pi, ct, level + 1); } }
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); } }
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; } }
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; } }