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> /// /// </summary> /// <param name="sourceElement"></param> /// <param name="targetElement"></param> /// <returns></returns> public static ConfigurationElement CloneElement(ConfigurationElement sourceElement, ConfigurationElement targetElement) { if (sourceElement is ICustomProviderData) { var targetAttributes = ((ICustomProviderData)targetElement).Attributes; var sourceAttributes = ((ICustomProviderData)sourceElement).Attributes; foreach (string key in sourceAttributes) { targetAttributes.Add(key, sourceAttributes[key]); } } foreach (PropertyInformation property in sourceElement.ElementInformation.Properties) { if (property.ValueOrigin == PropertyValueOrigin.Default) { continue; } try { if (property.Value == null) { continue; } } catch { continue; } PropertyInformation targetProperty = targetElement.ElementInformation.Properties[property.Name]; if (typeof(ConfigurationElement).IsAssignableFrom(property.Type) && typeof(ICloneableConfigurationElement).IsAssignableFrom(property.Type)) { targetProperty.Value = ((ICloneableConfigurationElement)property.Value).CreateFullClone(); } if (typeof(ConfigurationElement).IsAssignableFrom(property.Type) && TypeDescriptor.GetAttributes(property.Type).OfType <CloneableConfigurationElementTypeAttribute>().Any()) { CloneableConfigurationElementTypeAttribute cloneableConfigurationElementTypeAttribute = TypeDescriptor.GetAttributes(property.Type).OfType <CloneableConfigurationElementTypeAttribute>().First(); ICloneableConfigurationElement cloneable = (ICloneableConfigurationElement)Activator.CreateInstance(cloneableConfigurationElementTypeAttribute.CloneableConfigurationElementType, property.Value); targetProperty.Value = cloneable.CreateFullClone(); } if (typeof(ConfigurationElementCollection).IsAssignableFrom(property.Type)) { ConfigurationElementCollection sourceCollection = (ConfigurationElementCollection)property.Value; ConfigurationElementCollection targetCollection = (ConfigurationElementCollection)Activator.CreateInstance(sourceCollection.GetType()); targetCollection = CloneCollection(sourceCollection, targetCollection); targetProperty.Value = targetCollection; } else if (typeof(ConfigurationElement).IsAssignableFrom(property.Type)) { ConfigurationElement sourceChildElement = (ConfigurationElement)property.Value; ConfigurationElement targetChildElement = (ConfigurationElement)Activator.CreateInstance(sourceChildElement.GetType()); targetChildElement = CloneElement(sourceChildElement, targetChildElement); targetProperty.Value = targetChildElement; } else { targetProperty.Value = property.Value; } } return(targetElement); }