internal AutoConfigurationHelper(ConfigurationElement element, Action<ConfigurationProperty, object> valueSetter, Func<ConfigurationProperty, object> valueGetter) { _ConfigElement = element; _ValueSetter = valueSetter; _ValueGetter = valueGetter; var type = element.GetType(); Dictionary<ConfigurationProperty, PropertyInfo> properties; if (!_TypeProperties.TryGetValue(type, out properties)) { properties = new Dictionary<ConfigurationProperty, PropertyInfo>(); foreach (var member in type.GetProperties()) { var configField = member.GetCustomAttributes(typeof(ConfigurationPropertyAttribute), true).Cast<ConfigurationPropertyAttribute>().FirstOrDefault(); if (configField != null) { var property = new ConfigurationProperty(configField.Name, member.PropertyType, configField.DefaultValue, ConfigurationPropertyOptions.None); properties[property] = member; } } _TypeProperties.TryAdd(type, properties); } _Properties = properties; // Pre-initialize properties of type ConfigurationElement, or things go boom foreach (var property in _Properties) { if (typeof(ConfigurationElement).IsAssignableFrom(property.Value.PropertyType)) property.Value.SetValue(_ConfigElement, _ValueGetter(property.Key), null); } }
/// <summary> /// Overrides the base add method to link parents. /// </summary> /// <param name="element">The element.</param> /// <exception cref="ArgumentNullException"><paramref name="element"/> is <see langword="null" />.</exception> /// <exception cref="ConfigurationErrorsException">The element is of the wrong type.</exception> protected override void BaseAdd(System.Configuration.ConfigurationElement element) { if (element == null) { throw new ArgumentNullException(nameof(element)); } TValue value = element as TValue; if (value == null) { throw new ConfigurationErrorsException( string.Format( // ReSharper disable once AssignNullToNotNullAttribute Resources.ConfigurationElement_Init_Invalid_Configuration_Property_Type, element.GetType())); } TKey key = GetElementKey(value); string elementName = $"[{key}]"; IInternalConfigurationElement ice = value; lock (_children) { ice.Parent = this; ice.ConfigurationElementName = elementName; _children.Add(ice); base.BaseAdd(element); } ((IInternalConfigurationElement)this).OnChanged(this.GetFullPath(elementName)); }
private static IEnumerable<Argument> ReadConfigurationElement(ConfigurationElement element) { var arguments = new List<Argument>(); var collection = element as ConfigurationElementCollection; if (collection != null) { foreach (ConfigurationElement child in collection) { arguments.AddRange(ReadConfigurationElement(child)); } } else { foreach (PropertyInfo property in element.GetType().GetProperties()) { object propertyValue = property.GetValue(element, null); if (typeof (ConfigurationElement).IsAssignableFrom(property.PropertyType)) { arguments.AddRange(ReadConfigurationElement((ConfigurationElement) propertyValue)); } var argument = new Argument(property, propertyValue); if (argument.Include()) { arguments.Add(argument); } } } return arguments; }
protected override void Unmerge(ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode) { if (parentElement != null && sourceElement.GetType() != parentElement.GetType()) { throw new ConfigurationErrorsException("Can't unmerge two elements of different type"); } bool isMinimalOrModified = saveMode == ConfigurationSaveMode.Minimal || saveMode == ConfigurationSaveMode.Modified; foreach (PropertyInformation prop in sourceElement.ElementInformation.Properties) { if (prop.ValueOrigin == PropertyValueOrigin.Default) { continue; } PropertyInformation unmergedProp = ElementInformation.Properties [prop.Name]; object sourceValue = prop.Value; if (parentElement == null || !HasValue(parentElement, prop.Name)) { unmergedProp.Value = sourceValue; continue; } if (sourceValue == null) { continue; } object parentValue = GetItem(parentElement, prop.Name); if (!PropertyIsElement(prop)) { if (!object.Equals(sourceValue, parentValue) || (saveMode == ConfigurationSaveMode.Full) || (saveMode == ConfigurationSaveMode.Modified && prop.ValueOrigin == PropertyValueOrigin.SetHere)) { unmergedProp.Value = sourceValue; } continue; } var sourceElem = (ConfigurationElement)sourceValue; if (isMinimalOrModified && !ElementIsModified(sourceElem)) { continue; } if (parentValue == null) { unmergedProp.Value = sourceValue; continue; } var parentElem = (ConfigurationElement)parentValue; ConfigurationElement copy = (ConfigurationElement)unmergedProp.Value; ElementUnmerge(copy, sourceElem, parentElem, saveMode); } }
public void Register(ConfigurationElement element, Type @interface) { var type = element.GetType(); if (ClassAlreadyRegistered(type)) return; Register(element, new ConfigurationPropertyCollection(@interface, type).ToArray()); }
protected override void Register(ConfigurationElement element, params ConfigurationProperty[] configurationProperties) { var ownerType = element.GetType(); var properties = (SC.ConfigurationPropertyCollection)PropertyBagAccessor[ownerType]; if (properties == null) { properties = new SC.ConfigurationPropertyCollection(); PropertyBagAccessor[ownerType] = properties; } configurationProperties.ToList().ForEach(x => properties.Add(x)); }
protected internal virtual void Unmerge( ConfigurationElement source, ConfigurationElement parent, ConfigurationSaveMode updateMode) { if (parent != null && source.GetType() != parent.GetType()) { throw new ConfigurationException("Can't unmerge two elements of different type"); } foreach (PropertyInformation prop in source.ElementInformation.Properties) { if (prop.ValueOrigin == PropertyValueOrigin.Default) { continue; } PropertyInformation unmergedProp = ElementInformation.Properties [prop.Name]; object sourceValue = prop.Value; if (parent == null || !parent.HasValue(prop.Name)) { unmergedProp.Value = sourceValue; continue; } else if (sourceValue != null) { object parentValue = parent [prop.Name]; if (prop.IsElement) { if (parentValue != null) { ConfigurationElement copy = (ConfigurationElement)unmergedProp.Value; copy.Unmerge((ConfigurationElement)sourceValue, (ConfigurationElement)parentValue, updateMode); } else { unmergedProp.Value = sourceValue; } } else { if (!object.Equals(sourceValue, parentValue) || (updateMode == ConfigurationSaveMode.Full) || (updateMode == ConfigurationSaveMode.Modified && prop.ValueOrigin == PropertyValueOrigin.SetHere)) { unmergedProp.Value = sourceValue; } } } } }
public override bool Equals(object compareTo) { ConfigurationElement other = compareTo as ConfigurationElement; if (other == null) { return(false); } if (GetType() != other.GetType()) { return(false); } foreach (ConfigurationProperty prop in Properties) { if (!object.Equals(this [prop], other [prop])) { return(false); } } return(true); }
protected override void Register(ConfigurationElement element, params ConfigurationProperty[] configurationProperties) { lock (ElementMaps) { var ownerType = element.GetType(); var map = ElementMaps[ownerType]; if (map == null) { map = ElementMaps[ownerType] = ElementMapCreator(ownerType); } var properties = ElementMapPropertiesAccesor.GetValue(map) as SC.ConfigurationPropertyCollection; if (properties == null) { properties = new SC.ConfigurationPropertyCollection(); } ElementMapPropertiesAccesor.SetValue(map, properties); configurationProperties.ToList().ForEach(x => properties.Add(x)); var einfo = ElementInformationConstructor(element); ElementInformationAccessor.SetValue(element, einfo); } }
/// <summary> /// Gets the element key for a mediaObject element that uniquely identifies it. /// </summary> /// <param name="element">The ConfigurationElement to return the key for. </param> /// <returns>An Object that acts as the key for the specified ConfigurationElement.</returns> /// <exception cref="System.ArgumentException">Thrown when the element parameter is not of type /// MediaObject.</exception> protected override object GetElementKey(ConfigurationElement element) { MediaObject mediaObject = element as MediaObject; if (mediaObject == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.Global_GetElementKey_Ex_Msg, typeof(MediaObject).ToString(), element.GetType().ToString())); } return mediaObject.MimeType; }
/// <summary> /// Gets the element key for a browser element that uniquely identifies it. /// </summary> /// <param name="element">The <see cref="ConfigurationElement" /> to return the key for. </param> /// <returns>An Object that acts as the key for the specified <see cref="ConfigurationElement" />.</returns> /// <exception cref="System.ArgumentException">Thrown when the element parameter is not of type /// <see cref="Browser" />.</exception> protected override object GetElementKey(ConfigurationElement element) { Browser browser = element as Browser; if (browser == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.Global_GetElementKey_Ex_Msg, typeof(Browser).ToString(), element.GetType().ToString())); } return browser.Id; }
bool ElementIsModified(ConfigurationElement element) { return((bool)element.GetType().GetMethod("IsModified", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(element, new object [0])); }
void ElementUnmerge(ConfigurationElement target, ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode) { target.GetType().GetMethod("Unmerge", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(target, new object [] { sourceElement, parentElement, saveMode }); }
bool ElementIsModified (ConfigurationElement element) { return (bool) element.GetType ().GetMethod ("IsModified", BindingFlags.NonPublic | BindingFlags.Instance).Invoke (element, new object [0]); }
internal static void ValidateElement(ConfigurationElement elem, ConfigurationValidatorBase propValidator, bool recursive) { // Validate a config element with the per-type validator when a per-property ( propValidator ) is not supplied // or with the per-prop validator when the element ( elem ) is a child property of another configuration element ConfigurationValidatorBase validator = propValidator; if ((validator == null) && // Not a property - use the per-type validator (elem.ElementProperty != null)) { validator = elem.ElementProperty.Validator; // Since ElementProperty can be overriden by derived classes we need to make sure that // the validator supports the type of elem every time if ((validator != null) && !validator.CanValidate(elem.GetType())) { throw new ConfigurationErrorsException(string.Format(SR.Validator_does_not_support_elem_type, elem.GetType().Name)); } } try { validator?.Validate(elem); } catch (ConfigurationException) { // ConfigurationElement validators are allowed to throw ConfigurationErrorsException. throw; } catch (Exception ex) { throw new ConfigurationErrorsException(string.Format(SR.Validator_element_not_valid, elem.ElementTagName, ex.Message)); } if (recursive) { // Validate collection items: // Note: this is a bit of a hack - we will exploit the fact that this method is called with recursive == true only when serializing the top level section // At deserializtion time the per-element validator for collection items will get executed as part of their deserialization logic // However we dont perform validation in the serialization logic ( because at that time the object is unmerged and not all data is present ) // so we have to do that validation here. if (elem is ConfigurationElementCollection) { IEnumerator it = ((ConfigurationElementCollection)elem).GetElementsEnumerator(); while (it.MoveNext()) ValidateElement((ConfigurationElement)it.Current, null, true); } // Validate all child elements recursively for (int index = 0; index < elem.Values.Count; index++) { ConfigurationElement value = elem.Values[index] as ConfigurationElement; if (value != null) { // Run the per-type validator on the child element and proceed with validation in subelements // Note we dont run the per-property validator here since we run those when the property value is set ValidateElement(value, null, true); // per-type } } } }
private static void FillConfiguration(ConfigurationElement sectionElement, XElement rootElement) { var sectionType = sectionElement.GetType(); foreach (var property in sectionType.GetProperties()) { var configPropertyAttribute = property.GetCustomAttribute<ConfigurationPropertyAttribute>(); if (configPropertyAttribute == null) { continue; } var propertyElement = rootElement.Element(configPropertyAttribute.Name); if (typeof(ConfigurationElementCollection).IsAssignableFrom(property.PropertyType)) { var addMethod = property.PropertyType.GetMethod("Add", new Type[] { typeof(ConfigurationElement) }); if (addMethod == null) { return; } var configurationCollectionAttribute = property.GetCustomAttribute<ConfigurationCollectionAttribute>(); var elementCollection = (ConfigurationElementCollection)property.GetValue(sectionElement); var subElementName = string.IsNullOrEmpty(configurationCollectionAttribute.AddItemName) ? "add" : configurationCollectionAttribute.AddItemName; foreach (var subElement in propertyElement.Elements(subElementName)) { var elementInstance = (ConfigurationElement)configurationCollectionAttribute.ItemType.GetConstructor(new Type[] { }).Invoke(new object[] { }); FillConfiguration(elementInstance, subElement); addMethod.Invoke(elementCollection, new object[] { elementInstance }); } } else if (typeof(ConfigurationElement).IsAssignableFrom(property.PropertyType)) { var elementInstance = (ConfigurationElement)property.GetValue(sectionElement); FillConfiguration(elementInstance, propertyElement); } else { object value = null; if (propertyElement != null) { value = propertyElement.Value; } else { var attribute = rootElement.Attribute(configPropertyAttribute.Name); if (attribute != null) { value = attribute.Value; } else { continue; } } if (value != null) { var stringValue = value.ToString(); if (property.PropertyType.IsEnum) { property.SetValue(sectionElement, Enum.Parse(property.PropertyType, stringValue)); } else if (property.PropertyType == typeof(int)) { property.SetValue(sectionElement, int.Parse(stringValue)); } else { property.SetValue(sectionElement, stringValue); } } } } }
internal static void ValidateElement(ConfigurationElement elem, ConfigurationValidatorBase propValidator, bool recursive) { // Validate a config element with the per-type validator when a per-property ( propValidator ) is not supplied // or with the per-prop validator when the element ( elem ) is a child property of another configuration element ConfigurationValidatorBase validator = propValidator; if ((validator == null) && // Not a property - use the per-type validator (elem.ElementProperty != null)) { validator = elem.ElementProperty.Validator; // Since ElementProperty can be overriden by derived classes we need to make sure that // the validator supports the type of elem every time if ((validator != null) && !validator.CanValidate(elem.GetType())) { throw new ConfigurationErrorsException(SR.GetString(SR.Validator_does_not_support_elem_type, elem.GetType().Name)); } } try { if (validator != null) { validator.Validate(elem); } } catch (ConfigurationException) { // ConfigurationElement validators are allowed to throw ConfigurationErrorsException. throw; } catch (Exception ex) { throw new ConfigurationErrorsException(SR.GetString(SR.Validator_element_not_valid, elem._elementTagName, ex.Message)); } catch { throw new ConfigurationErrorsException(SR.GetString(SR.Validator_element_not_valid, elem._elementTagName, ExceptionUtil.NoExceptionInformation)); } if (recursive == true) { if (elem is ConfigurationElementCollection) { if (elem is ConfigurationElementCollection) { IEnumerator it = ((ConfigurationElementCollection)elem).GetElementsEnumerator(); while( it.MoveNext() ) { ValidateElement((ConfigurationElement)it.Current, null, true); } } } // Validate all child elements recursively for (int index = 0; index < elem.Values.Count; index++) { ConfigurationElement value = elem.Values[index] as ConfigurationElement; if (value != null) { // Run the per-type validator on the child element and proceed with validation in subelements // Note we dont run the per-property validator here since we run those when the property value is set ValidateElement(value, null, true); // per-type } } } }
public ConfigurationElementMerge(ConfigurationElement parentElement, ConfigurationElement localElement) { if (parentElement.GetType() != localElement.GetType()) { string message = String.Format(CultureInfo.CurrentCulture, Resources.ExceptionIncompaitbleMergeElementType, localElement.GetType(), parentElement.GetType()); throw new ConfigurationSourceErrorsException(message); } this.parentElement = parentElement; this.localElement = localElement; }
internal static void ValidateElement(ConfigurationElement elem, ConfigurationValidatorBase propValidator, bool recursive) { ConfigurationValidatorBase validator = propValidator; if ((validator == null) && (elem.ElementProperty != null)) { validator = elem.ElementProperty.Validator; if ((validator != null) && !validator.CanValidate(elem.GetType())) { throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Validator_does_not_support_elem_type", new object[] { elem.GetType().Name })); } } try { if (validator != null) { validator.Validate(elem); } } catch (ConfigurationException) { throw; } catch (Exception exception) { throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Validator_element_not_valid", new object[] { elem._elementTagName, exception.Message })); } if (recursive) { if ((elem is ConfigurationElementCollection) && (elem is ConfigurationElementCollection)) { IEnumerator elementsEnumerator = ((ConfigurationElementCollection) elem).GetElementsEnumerator(); while (elementsEnumerator.MoveNext()) { ValidateElement((ConfigurationElement) elementsEnumerator.Current, null, true); } } for (int i = 0; i < elem.Values.Count; i++) { ConfigurationElement element = elem.Values[i] as ConfigurationElement; if (element != null) { ValidateElement(element, null, true); } } } }
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; }
internal static void ValidateElement(ConfigurationElement elem, ConfigurationValidatorBase propValidator, bool recursive) { ConfigurationValidatorBase configurationValidatorBase = propValidator; if (configurationValidatorBase == null) { if (elem.ElementProperty != null) { configurationValidatorBase = elem.ElementProperty.Validator; if (configurationValidatorBase != null) { if (!configurationValidatorBase.CanValidate(elem.GetType())) throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Validator_does_not_support_elem_type", new object[1] { (object) elem.GetType().Name })); } } } try { if (configurationValidatorBase != null) configurationValidatorBase.Validate((object) elem); } catch (ConfigurationException ex) { throw; } catch (Exception ex) { throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Validator_element_not_valid", (object) elem._elementTagName, (object) ex.Message)); } if (!recursive) return; if (elem is ConfigurationElementCollection && elem is ConfigurationElementCollection) { IEnumerator elementsEnumerator = ((ConfigurationElementCollection) elem).GetElementsEnumerator(); while (elementsEnumerator.MoveNext()) ConfigurationElement.ValidateElement((ConfigurationElement) elementsEnumerator.Current, (ConfigurationValidatorBase) null, true); } for (int index = 0; index < elem.Values.Count; ++index) { ConfigurationElement elem1 = elem.Values[index] as ConfigurationElement; if (elem1 != null) ConfigurationElement.ValidateElement(elem1, (ConfigurationValidatorBase) null, true); } }
protected internal virtual void Unmerge ( ConfigurationElement source, ConfigurationElement parent, ConfigurationSaveMode updateMode) { if (parent != null && source.GetType() != parent.GetType()) throw new ConfigurationErrorsException ("Can't unmerge two elements of different type"); bool isMinimalOrModified = updateMode == ConfigurationSaveMode.Minimal || updateMode == ConfigurationSaveMode.Modified; foreach (PropertyInformation prop in source.ElementInformation.Properties) { if (prop.ValueOrigin == PropertyValueOrigin.Default) continue; PropertyInformation unmergedProp = ElementInformation.Properties [prop.Name]; object sourceValue = prop.Value; if (parent == null || !parent.HasValue (prop.Name)) { unmergedProp.Value = sourceValue; continue; } if (sourceValue == null) continue; object parentValue = parent [prop.Name]; if (!prop.IsElement) { if (!object.Equals (sourceValue, parentValue) || (updateMode == ConfigurationSaveMode.Full) || (updateMode == ConfigurationSaveMode.Modified && prop.ValueOrigin == PropertyValueOrigin.SetHere)) unmergedProp.Value = sourceValue; continue; } var sourceElement = (ConfigurationElement) sourceValue; if (isMinimalOrModified && !sourceElement.IsModified ()) continue; if (parentValue == null) { unmergedProp.Value = sourceValue; continue; } var parentElement = (ConfigurationElement) parentValue; ConfigurationElement copy = (ConfigurationElement) unmergedProp.Value; copy.Unmerge (sourceElement, parentElement, updateMode); } }
/// <summary> /// Gets the element key for a mimeType element that uniquely identifies it. /// </summary> /// <param name="element">The <see cref="ConfigurationElement" /> to return the key for. </param> /// <returns>An Object that acts as the key for the specified <see cref="ConfigurationElement" />.</returns> /// <exception cref="System.ArgumentException">Thrown when the element parameter is not of type /// <see cref="MimeType" />.</exception> protected override object GetElementKey(ConfigurationElement element) { MimeType mimeType = element as MimeType; if (mimeType == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.Global_GetElementKey_Ex_Msg, typeof(MimeType).ToString(), element.GetType().ToString())); } return String.Concat(mimeType.FileExtension, "|", mimeType.BrowserId); }
public string GetTagForExtensionElement(ConfigurationElement element) { Type elementType = element.GetType(); Dictionary<string, Type> dictToSearch = GetDictToSearch(elementType); foreach(var keyValue in dictToSearch) { if (keyValue.Value == elementType) { return keyValue.Key; } } throw ElementTypeNotFound(elementType); }
/// <summary> /// Retrieves a key of a given element. /// </summary> /// <param name="element">Element whose key to retrieve.</param> /// <returns>A key of a given element.</returns> protected override object GetElementKey(ConfigurationElement element) { object ret = null; if (element != null && Type.Equals(element.GetType(), typeof(CultureConfiguration))) ret = ((CultureConfiguration)element).CultureName; return ret; }
private static void ApplyValidator(ConfigurationElement elem) { if (ConfigurationElement.s_perTypeValidators == null || !ConfigurationElement.s_perTypeValidators.ContainsKey(elem.GetType())) return; elem._elementProperty = new ConfigurationElementProperty(ConfigurationElement.s_perTypeValidators[elem.GetType()]); }
protected override void Unmerge (ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode) { if (parentElement != null && sourceElement.GetType() != parentElement.GetType()) throw new ConfigurationErrorsException ("Can't unmerge two elements of different type"); bool isMinimalOrModified = saveMode == ConfigurationSaveMode.Minimal || saveMode == ConfigurationSaveMode.Modified; foreach (PropertyInformation prop in sourceElement.ElementInformation.Properties) { if (prop.ValueOrigin == PropertyValueOrigin.Default) continue; PropertyInformation unmergedProp = ElementInformation.Properties [prop.Name]; object sourceValue = prop.Value; if (parentElement == null || !HasValue (parentElement, prop.Name)) { unmergedProp.Value = sourceValue; continue; } if (sourceValue == null) continue; object parentValue = GetItem (parentElement, prop.Name); if (!PropertyIsElement (prop)) { if (!object.Equals (sourceValue, parentValue) || (saveMode == ConfigurationSaveMode.Full) || (saveMode == ConfigurationSaveMode.Modified && prop.ValueOrigin == PropertyValueOrigin.SetHere)) unmergedProp.Value = sourceValue; continue; } var sourceElem = (ConfigurationElement) sourceValue; if (isMinimalOrModified && !ElementIsModified (sourceElem)) continue; if (parentValue == null) { unmergedProp.Value = sourceValue; continue; } var parentElem = (ConfigurationElement) parentValue; ConfigurationElement copy = (ConfigurationElement) unmergedProp.Value; ElementUnmerge (copy, sourceElem, parentElem, saveMode); } }
internal static void SetIsPresent(ConfigurationElement element) { // Work around for VSW 578830: ConfigurationElements that override DeserializeElement cannot set ElementInformation.IsPresent PropertyInfo elementPresent = element.GetType().GetProperty("ElementPresent", BindingFlags.Instance | BindingFlags.NonPublic); SetIsPresentWithAssert(elementPresent, element, true); }
void ElementUnmerge (ConfigurationElement target, ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode) { target.GetType ().GetMethod ("Unmerge", BindingFlags.NonPublic | BindingFlags.Instance).Invoke (target, new object [] {sourceElement, parentElement, saveMode}); }
private void MergeBasedOnConfig(XElement mergedElement, XElement partElement, ConfigurationElement configElement) { var defaultCollectionProperty = configElement.GetType() .GetProperties() .FirstOrDefault(p => { var attr = (ConfigurationPropertyAttribute) Attribute.GetCustomAttribute(p, typeof (ConfigurationPropertyAttribute)); return attr!=null && attr.IsDefaultCollection; }); if (defaultCollectionProperty != null) { var configCollectionAttribute = (ConfigurationCollectionAttribute)Attribute.GetCustomAttribute(defaultCollectionProperty.PropertyType, typeof (ConfigurationCollectionAttribute)); if (configCollectionAttribute != null) { var keyProperty = configCollectionAttribute.ItemType.GetProperties() .Select(p => new { Property = p, Attribute = (ConfigurationPropertyAttribute) Attribute.GetCustomAttribute(p, typeof ( ConfigurationPropertyAttribute )) }) .FirstOrDefault(p => p.Attribute != null && p.Attribute.IsKey); if (keyProperty != null) { MergeAttributes(mergedElement, partElement); var mergedElements = mergedElement.Nodes().OfType<XElement>().ToList(); foreach (var partChildElement in partElement.Nodes().OfType<XElement>()) { var key = partChildElement.Attributes() .FirstOrDefault( a => a.Name.LocalName.Equals(keyProperty.Attribute.Name, StringComparison.OrdinalIgnoreCase)); if (key == null) continue; var mergedChildElement = mergedElements .FirstOrDefault(e => { var keyAttribute = e.Attribute(key.Name); return keyAttribute != null && key.Value.Equals(keyAttribute.Value); }); if (mergedChildElement != null) mergedElements.Remove(mergedChildElement); mergedElements.Add(partChildElement); } mergedElement.ReplaceNodes(mergedElements); return; } } } // TODO: actually, we should dig into each ConfigurationElement, but we'll skip it for now MergeAsRawXml(mergedElement, partElement); }
private static void ApplyValidator(ConfigurationElement elem) { if ((s_perTypeValidators != null) && s_perTypeValidators.ContainsKey(elem.GetType())) { elem._elementProperty = new ConfigurationElementProperty(s_perTypeValidators[elem.GetType()]); } }
internal static void SetIsPresent(ConfigurationElement element) { SetIsPresentWithAssert(element.GetType().GetProperty("ElementPresent", BindingFlags.NonPublic | BindingFlags.Instance), element, true); }