SourceElement
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Xml;
namespace System.Diagnostics
{
internal sealed class SourceElement : ConfigurationElement
{
private static readonly ConfigurationPropertyCollection _properties;
private static readonly ConfigurationProperty _propName;
private static readonly ConfigurationProperty _propSwitchName;
private static readonly ConfigurationProperty _propSwitchValue;
private static readonly ConfigurationProperty _propSwitchType;
private static readonly ConfigurationProperty _propListeners;
private StringDictionary _attributes;
public StringDictionary Attributes => _attributes ?? (_attributes = new StringDictionary());
[ConfigurationProperty("listeners")]
public ListenerElementsCollection Listeners {
get {
return (ListenerElementsCollection)base[_propListeners];
}
}
[ConfigurationProperty("name", IsRequired = true, DefaultValue = "")]
public string Name {
get {
return (string)base[_propName];
}
}
protected internal override ConfigurationPropertyCollection Properties => _properties;
[ConfigurationProperty("switchName")]
public string SwitchName {
get {
return (string)base[_propSwitchName];
}
}
[ConfigurationProperty("switchValue")]
public string SwitchValue {
get {
return (string)base[_propSwitchValue];
}
}
[ConfigurationProperty("switchType")]
public string SwitchType {
get {
return (string)base[_propSwitchType];
}
}
static SourceElement()
{
_properties = new ConfigurationPropertyCollection();
_propName = new ConfigurationProperty("name", typeof(string), "", ConfigurationPropertyOptions.IsRequired);
_propSwitchName = new ConfigurationProperty("switchName", typeof(string), null, ConfigurationPropertyOptions.None);
_propSwitchValue = new ConfigurationProperty("switchValue", typeof(string), null, ConfigurationPropertyOptions.None);
_propSwitchType = new ConfigurationProperty("switchType", typeof(string), null, ConfigurationPropertyOptions.None);
_propListeners = new ConfigurationProperty("listeners", typeof(ListenerElementsCollection), new ListenerElementsCollection(), ConfigurationPropertyOptions.None);
_properties.Add(_propName);
_properties.Add(_propSwitchName);
_properties.Add(_propSwitchValue);
_properties.Add(_propSwitchType);
_properties.Add(_propListeners);
}
protected internal override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
{
base.DeserializeElement(reader, serializeCollectionKey);
if (!string.IsNullOrEmpty(SwitchName) && !string.IsNullOrEmpty(SwitchValue))
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Only_specify_one, Name));
}
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)
{
if (!base.SerializeElement(writer, serializeCollectionKey)) {
if (_attributes != null)
return _attributes.Count > 0;
return false;
}
return true;
}
protected internal override void Unmerge(ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
{
base.Unmerge(sourceElement, parentElement, saveMode);
SourceElement sourceElement2 = sourceElement as SourceElement;
if (sourceElement2 != null && sourceElement2._attributes != null)
_attributes = sourceElement2._attributes;
}
internal void ResetProperties()
{
if (_attributes != null) {
_attributes.Clear();
_properties.Clear();
_properties.Add(_propName);
_properties.Add(_propSwitchName);
_properties.Add(_propSwitchValue);
_properties.Add(_propSwitchType);
_properties.Add(_propListeners);
}
}
}
}