private static ConfigurationElementCollection CloneCollection(ConfigurationElementCollection sourceCollection, ConfigurationElementCollection targetCollection) { targetCollection = (ConfigurationElementCollection)CloneElement(sourceCollection, targetCollection); IMergeableConfigurationElementCollection mergeableSource = MergeableConfigurationCollectionFactory.GetCreateMergeableCollection(sourceCollection); IMergeableConfigurationElementCollection mergeableTarget = MergeableConfigurationCollectionFactory.GetCreateMergeableCollection(targetCollection); if (mergeableSource == null) { return(targetCollection); } List <ConfigurationElement> targetCollectionContents = new List <ConfigurationElement>(); foreach (ConfigurationElement sourceElement in sourceCollection) { ConfigurationElement targetElement; targetElement = CreateCopyOfCollectionElement(mergeableSource, sourceElement); targetElement = CloneElement(sourceElement, targetElement); targetCollectionContents.Add(targetElement); } mergeableTarget.ResetCollection(targetCollectionContents); targetCollection.EmitClear = sourceCollection.EmitClear; return(targetCollection); }
public ConfigurationElementCollection GetMergedElementCollection() { if (mergeableLocalElement == null) { return(localCollection); } if (localCollection.EmitClear) { return(localCollection); } List <ConfigurationElement> elementsFromParent = parentCollection.Cast <ConfigurationElement>().ToList(); foreach (ConfigurationElement elementFromLocal in localCollection.Cast <ConfigurationElement>()) { var keyPredicate = CreateMatchKeyPredicate(elementFromLocal); ConfigurationElement matchingElement = elementsFromParent.Where(keyPredicate).FirstOrDefault(); if (matchingElement != null) { ConfigurationElementMerge elementMerge = new ConfigurationElementMerge(matchingElement, elementFromLocal); ConfigurationElement mergedElement = elementMerge.GetMergedElement(); int index = elementsFromParent.IndexOf(matchingElement); elementsFromParent.RemoveAt(index); elementsFromParent.Insert(index, mergedElement); } else { elementsFromParent.Add(elementFromLocal); } } mergeableLocalElement.ResetCollection(elementsFromParent); return(localCollection); }
/// <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); }