123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- using System;
- using System.Configuration;
- using System.Collections.Specialized;
- using System.Xml;
- namespace SuperSocket.Common
- {
-
-
-
- [Serializable]
- public class ConfigurationElementBase : ConfigurationElement
- {
- private bool m_NameRequired;
-
-
-
- public ConfigurationElementBase()
- : this(true)
- {
- }
-
-
-
-
- public ConfigurationElementBase(bool nameRequired)
- {
- m_NameRequired = nameRequired;
- Options = new NameValueCollection();
- }
-
-
-
- [ConfigurationProperty("name")]
- public string Name
- {
- get { return this["name"] as string; }
- }
-
-
-
-
-
-
- protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
- {
- base.DeserializeElement(reader, serializeCollectionKey);
- if (m_NameRequired && string.IsNullOrEmpty(Name))
- {
- throw new ConfigurationErrorsException("Required attribute 'name' not found.");
- }
- }
-
-
-
- public NameValueCollection Options { get; set; }
-
-
-
-
-
-
-
-
- protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
- {
- Options.Add(name, value);
- return true;
- }
-
-
-
-
-
-
- protected override void Unmerge(ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
- {
- base.Unmerge(sourceElement, parentElement, saveMode);
- var element = sourceElement as ConfigurationElementBase;
- if (element == null)
- return;
- if (element.Options != this.Options)
- this.Options = element.Options;
- if (element.OptionElements != this.OptionElements)
- this.OptionElements = element.OptionElements;
- }
-
-
-
-
-
-
-
-
- protected override bool SerializeElement(XmlWriter writer, bool serializeCollectionKey)
- {
- if (!base.SerializeElement(writer, serializeCollectionKey))
- return false;
- if (writer == null)
- return true;
- var options = Options;
- if (options != null && options.Count > 0)
- {
- for (var i = 0; i < options.Count; i++)
- {
- writer.WriteAttributeString(options.GetKey(i), options.Get(i));
- }
- }
- var optionElements = OptionElements;
- if (optionElements != null && optionElements.Count > 0)
- {
- for (var i = 0; i < optionElements.Count; i++)
- {
- writer.WriteRaw(optionElements.Get(i));
- }
- }
- return true;
- }
-
-
-
- public NameValueCollection OptionElements { get; set; }
-
-
-
-
-
-
-
-
-
- protected override bool OnDeserializeUnrecognizedElement(string elementName, System.Xml.XmlReader reader)
- {
- if (OptionElements == null)
- OptionElements = new NameValueCollection();
- OptionElements.Add(elementName, reader.ReadOuterXml());
- return true;
- }
- }
- }
|