ConfigurationElementBase.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Configuration;
  3. using System.Collections.Specialized;
  4. using System.Xml;
  5. namespace SuperSocket.Common
  6. {
  7. /// <summary>
  8. /// ConfigurationElementBase
  9. /// </summary>
  10. [Serializable]
  11. public class ConfigurationElementBase : ConfigurationElement
  12. {
  13. private bool m_NameRequired;
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="ConfigurationElementBase"/> class.
  16. /// </summary>
  17. public ConfigurationElementBase()
  18. : this(true)
  19. {
  20. }
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="ConfigurationElementBase"/> class.
  23. /// </summary>
  24. /// <param name="nameRequired">if set to <c>true</c> [name required].</param>
  25. public ConfigurationElementBase(bool nameRequired)
  26. {
  27. m_NameRequired = nameRequired;
  28. Options = new NameValueCollection();
  29. }
  30. /// <summary>
  31. /// Gets the name.
  32. /// </summary>
  33. [ConfigurationProperty("name")]
  34. public string Name
  35. {
  36. get { return this["name"] as string; }
  37. }
  38. /// <summary>
  39. /// Reads XML from the configuration file.
  40. /// </summary>
  41. /// <param name="reader">The <see cref="T:System.Xml.XmlReader"/> that reads from the configuration file.</param>
  42. /// <param name="serializeCollectionKey">true to serialize only the collection key properties; otherwise, false.</param>
  43. /// <exception cref="T:System.Configuration.ConfigurationErrorsException">The element to read is locked.- or -An attribute of the current node is not recognized.- or -The lock status of the current node cannot be determined. </exception>
  44. protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
  45. {
  46. base.DeserializeElement(reader, serializeCollectionKey);
  47. if (m_NameRequired && string.IsNullOrEmpty(Name))
  48. {
  49. throw new ConfigurationErrorsException("Required attribute 'name' not found.");
  50. }
  51. }
  52. /// <summary>
  53. /// Gets the options.
  54. /// </summary>
  55. public NameValueCollection Options { get; set; }
  56. /// <summary>
  57. /// Gets a value indicating whether an unknown attribute is encountered during deserialization.
  58. /// </summary>
  59. /// <param name="name">The name of the unrecognized attribute.</param>
  60. /// <param name="value">The value of the unrecognized attribute.</param>
  61. /// <returns>
  62. /// true when an unknown attribute is encountered while deserializing; otherwise, false.
  63. /// </returns>
  64. protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
  65. {
  66. Options.Add(name, value);
  67. return true;
  68. }
  69. /// <summary>
  70. /// Modifies the <see cref="T:System.Configuration.ConfigurationElement" /> object to remove all values that should not be saved.
  71. /// </summary>
  72. /// <param name="sourceElement">A <see cref="T:System.Configuration.ConfigurationElement" /> at the current level containing a merged view of the properties.</param>
  73. /// <param name="parentElement">The parent <see cref="T:System.Configuration.ConfigurationElement" />, or null if this is the top level.</param>
  74. /// <param name="saveMode">A <see cref="T:System.Configuration.ConfigurationSaveMode" /> that determines which property values to include.</param>
  75. protected override void Unmerge(ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
  76. {
  77. base.Unmerge(sourceElement, parentElement, saveMode);
  78. var element = sourceElement as ConfigurationElementBase;
  79. if (element == null)
  80. return;
  81. if (element.Options != this.Options)
  82. this.Options = element.Options;
  83. if (element.OptionElements != this.OptionElements)
  84. this.OptionElements = element.OptionElements;
  85. }
  86. /// <summary>
  87. /// Writes the contents of this configuration element to the configuration file when implemented in a derived class.
  88. /// </summary>
  89. /// <param name="writer">The <see cref="T:System.Xml.XmlWriter" /> that writes to the configuration file.</param>
  90. /// <param name="serializeCollectionKey">true to serialize only the collection key properties; otherwise, false.</param>
  91. /// <returns>
  92. /// true if any data was actually serialized; otherwise, false.
  93. /// </returns>
  94. protected override bool SerializeElement(XmlWriter writer, bool serializeCollectionKey)
  95. {
  96. if (!base.SerializeElement(writer, serializeCollectionKey))
  97. return false;
  98. if (writer == null)
  99. return true;
  100. var options = Options;
  101. if (options != null && options.Count > 0)
  102. {
  103. for (var i = 0; i < options.Count; i++)
  104. {
  105. writer.WriteAttributeString(options.GetKey(i), options.Get(i));
  106. }
  107. }
  108. var optionElements = OptionElements;
  109. if (optionElements != null && optionElements.Count > 0)
  110. {
  111. for (var i = 0; i < optionElements.Count; i++)
  112. {
  113. writer.WriteRaw(optionElements.Get(i));
  114. }
  115. }
  116. return true;
  117. }
  118. /// <summary>
  119. /// Gets the option elements.
  120. /// </summary>
  121. public NameValueCollection OptionElements { get; set; }
  122. /// <summary>
  123. /// Gets a value indicating whether an unknown element is encountered during deserialization.
  124. /// </summary>
  125. /// <param name="elementName">The name of the unknown subelement.</param>
  126. /// <param name="reader">The <see cref="T:System.Xml.XmlReader"/> being used for deserialization.</param>
  127. /// <returns>
  128. /// true when an unknown element is encountered while deserializing; otherwise, false.
  129. /// </returns>
  130. /// <exception cref="T:System.Configuration.ConfigurationErrorsException">The element identified by <paramref name="elementName"/> is locked.- or -One or more of the element's attributes is locked.- or -<paramref name="elementName"/> is unrecognized, or the element has an unrecognized attribute.- or -The element has a Boolean attribute with an invalid value.- or -An attempt was made to deserialize a property more than once.- or -An attempt was made to deserialize a property that is not a valid member of the element.- or -The element cannot contain a CDATA or text element.</exception>
  131. protected override bool OnDeserializeUnrecognizedElement(string elementName, System.Xml.XmlReader reader)
  132. {
  133. if (OptionElements == null)
  134. OptionElements = new NameValueCollection();
  135. OptionElements.Add(elementName, reader.ReadOuterXml());
  136. return true;
  137. }
  138. }
  139. }