private static ConfigurationElement CreateCopyOfCollectionElement(IMergeableConfigurationElementCollection mergeableSource, ConfigurationElement sourceElement) { ConfigurationElement targetElement; Type sourceType = sourceElement.GetType(); if (sourceElement is ICloneableConfigurationElement) { return(((ICloneableConfigurationElement)sourceElement).CreateFullClone()); } if (TypeDescriptor.GetAttributes(sourceElement).OfType <CloneableConfigurationElementTypeAttribute>().Any()) { CloneableConfigurationElementTypeAttribute cloneableConfigurationElementTypeAttribute = TypeDescriptor.GetAttributes(sourceElement).OfType <CloneableConfigurationElementTypeAttribute>().First(); ICloneableConfigurationElement cloneable = (ICloneableConfigurationElement)Activator.CreateInstance(cloneableConfigurationElementTypeAttribute.CloneableConfigurationElementType, sourceElement); return(cloneable.CreateFullClone()); } targetElement = mergeableSource.CreateNewElement(sourceType); return(targetElement); }
/// <summary> /// Adds a new item of type <paramref name="elementType"/> to the collection. /// </summary> /// <param name="elementType">The <see cref="Type"/> of element to add. It should be of, or derive from, the type indicated in <see cref="CollectionElementType"/>.</param> /// <returns>An <see cref="ElementViewModel"/> for the added type.</returns> public virtual ElementViewModel AddNewCollectionElement(Type elementType) { EnsureHasChildElements(); var element = mergeableConfigurationCollection.CreateNewElement(elementType); var childElementModel = ContainingSection.CreateCollectionElement(this, element); if (childElementModel.NameProperty != null) { childElementModel.NameProperty.Value = CalculateNameFromType(elementType); } // add the new element to the configuration. mergeableConfigurationCollection.ResetCollection( thisElementCollection.OfType <ConfigurationElement>() .Concat(new[] { element }) .ToArray()); foreach (var property in childElementModel.Properties) { DesigntimeDefaultAttribute defaultDesigntimeValue = property.Attributes.OfType <DesigntimeDefaultAttribute>().FirstOrDefault(); if (defaultDesigntimeValue != null) { property.Value = property.ConvertFromBindableValueInvariant(defaultDesigntimeValue.BindableDefaultValue); } } childElementModel.Initialize(new InitializeContext()); InitializeElementProperties(childElementModel); // add the new element to the view model. AddChildToView(childElementModel); Validate(); return(childElementModel); }
private static ConfigurationElement CreateCopyOfCollectionElement(IMergeableConfigurationElementCollection mergeableSource, ConfigurationElement sourceElement) { ConfigurationElement targetElement; Type sourceType = sourceElement.GetType(); if (sourceElement is ICloneableConfigurationElement) { return ((ICloneableConfigurationElement)sourceElement).CreateFullClone(); } if (TypeDescriptor.GetAttributes(sourceElement).OfType<CloneableConfigurationElementTypeAttribute>().Any()) { CloneableConfigurationElementTypeAttribute cloneableConfigurationElementTypeAttribute = TypeDescriptor.GetAttributes(sourceElement).OfType<CloneableConfigurationElementTypeAttribute>().First(); ICloneableConfigurationElement cloneable = (ICloneableConfigurationElement)Activator.CreateInstance(cloneableConfigurationElementTypeAttribute.CloneableConfigurationElementType, sourceElement); return cloneable.CreateFullClone(); } targetElement = mergeableSource.CreateNewElement(sourceType); return targetElement; }