SwitchElement
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Xml;
namespace System.Diagnostics
{
internal sealed class SwitchElement : ConfigurationElement
{
private static readonly ConfigurationPropertyCollection _properties;
private static readonly ConfigurationProperty _propName;
private static readonly ConfigurationProperty _propValue;
private StringDictionary _attributes;
public StringDictionary Attributes => _attributes ?? (_attributes = new StringDictionary());
[ConfigurationProperty("name", DefaultValue = "", IsRequired = true, IsKey = true)]
public string Name {
get {
return (string)base[_propName];
}
}
protected internal override ConfigurationPropertyCollection Properties => _properties;
[ConfigurationProperty("value", IsRequired = true)]
public string Value {
get {
return (string)base[_propValue];
}
}
static SwitchElement()
{
_properties = new ConfigurationPropertyCollection();
_propName = new ConfigurationProperty("name", typeof(string), "", ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey);
_propValue = new ConfigurationProperty("value", typeof(string), null, ConfigurationPropertyOptions.IsRequired);
_properties.Add(_propName);
_properties.Add(_propValue);
}
protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
{
Attributes.Add(name, value);
return true;
}
protected override void PreSerialize(XmlWriter writer)
{
if (_attributes != null) {
IDictionaryEnumerator dictionaryEnumerator = (IDictionaryEnumerator)_attributes.GetEnumerator();
while (dictionaryEnumerator.MoveNext()) {
string text = (string)dictionaryEnumerator.Value;
string localName = (string)dictionaryEnumerator.Key;
if (text != null)
writer?.WriteAttributeString(localName, text);
}
}
}
protected internal override bool SerializeElement(XmlWriter writer, bool serializeCollectionKey)
{
return base.SerializeElement(writer, serializeCollectionKey) || (_attributes != null && _attributes.Count > 0);
}
protected internal override void Unmerge(ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
{
base.Unmerge(sourceElement, parentElement, saveMode);
SwitchElement switchElement = sourceElement as SwitchElement;
if (switchElement != null && switchElement._attributes != null)
_attributes = switchElement._attributes;
}
internal void ResetProperties()
{
if (_attributes != null) {
_attributes.Clear();
_properties.Clear();
_properties.Add(_propName);
_properties.Add(_propValue);
}
}
}
}