The Labs \ Source Viewer \ SSCLI \ System.Configuration \ SettingValueElement

  1. //------------------------------------------------------------------------------
  2. // <copyright file="ClientSettingsSection.cs" company="Microsoft Corporation">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Configuration
  16. {
  17.     using System;
  18.     using System.Xml;
  19.     using System.Configuration;
  20.     using System.Collections.Specialized;
  21.     using System.Collections;
  22.     using System.IO;
  23.     using System.Text;
  24.    
  25.     /// <devdoc>
  26.     /// ConfigurationSection class for sections that store client settings.
  27.     /// </devdoc>
  28.     public sealed class ClientSettingsSection : ConfigurationSection
  29.     {
  30.         private static ConfigurationPropertyCollection _properties;
  31.         private static readonly ConfigurationProperty _propSettings = new ConfigurationProperty(null, typeof(SettingElementCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);
  32.        
  33.         static ClientSettingsSection()
  34.         {
  35.             _properties = new ConfigurationPropertyCollection();
  36.             _properties.Add(_propSettings);
  37.         }
  38.        
  39.         public ClientSettingsSection()
  40.         {
  41.         }
  42.        
  43.         protected override ConfigurationPropertyCollection Properties {
  44.             get { return _properties; }
  45.         }
  46.        
  47.         /// <include file='doc\ClientSettingsSection.uex' path='docs/doc[@for="ClientSettingsSection.Settings]/*' />
  48.         [ConfigurationProperty("", IsDefaultCollection = true)]
  49.         public SettingElementCollection Settings {
  50.             get { return (SettingElementCollection)base[_propSettings]; }
  51.         }
  52.     }
  53.    
  54.     public sealed class SettingElementCollection : ConfigurationElementCollection
  55.     {
  56.         public override ConfigurationElementCollectionType CollectionType {
  57.             get { return ConfigurationElementCollectionType.BasicMap; }
  58.         }
  59.        
  60.         protected override string ElementName {
  61.             get { return "setting"; }
  62.         }
  63.        
  64.         protected override ConfigurationElement CreateNewElement()
  65.         {
  66.             return new SettingElement();
  67.         }
  68.        
  69.         protected override object GetElementKey(ConfigurationElement element)
  70.         {
  71.             return ((SettingElement)element).Key;
  72.         }
  73.        
  74.         public SettingElement Get(string elementKey)
  75.         {
  76.             return (SettingElement)BaseGet(elementKey);
  77.         }
  78.        
  79.         public void Add(SettingElement element)
  80.         {
  81.             BaseAdd(element);
  82.         }
  83.        
  84.         public void Remove(SettingElement element)
  85.         {
  86.             BaseRemove(GetElementKey(element));
  87.         }
  88.        
  89.         public void Clear()
  90.         {
  91.             BaseClear();
  92.         }
  93.     }
  94.    
  95.     public sealed class SettingElement : ConfigurationElement
  96.     {
  97.         private static ConfigurationPropertyCollection _properties;
  98.         private static readonly ConfigurationProperty _propName = new ConfigurationProperty("name", typeof(string), "", ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey);
  99.         private static readonly ConfigurationProperty _propSerializeAs = new ConfigurationProperty("serializeAs", typeof(SettingsSerializeAs), SettingsSerializeAs.String, ConfigurationPropertyOptions.IsRequired);
  100.         private static readonly ConfigurationProperty _propValue = new ConfigurationProperty("value", typeof(SettingValueElement), null, ConfigurationPropertyOptions.IsRequired);
  101.         private static XmlDocument doc = new XmlDocument();
  102.        
  103.         static SettingElement()
  104.         {
  105.             // Property initialization
  106.             _properties = new ConfigurationPropertyCollection();
  107.            
  108.             _properties.Add(_propName);
  109.             _properties.Add(_propSerializeAs);
  110.             _properties.Add(_propValue);
  111.            
  112.         }
  113.        
  114.         public SettingElement()
  115.         {
  116.         }
  117.        
  118.         public SettingElement(string name, SettingsSerializeAs serializeAs) : this()
  119.         {
  120.             Name = name;
  121.             SerializeAs = serializeAs;
  122.         }
  123.        
  124.         internal string Key {
  125.             get { return Name; }
  126.         }
  127.        
  128.         public override bool Equals(object settings)
  129.         {
  130.             SettingElement u = settings as SettingElement;
  131.             return (u != null && base.Equals(settings) && Object.Equals(u.Value, Value));
  132.         }
  133.        
  134.         public override int GetHashCode()
  135.         {
  136.             return base.GetHashCode() ^ Value.GetHashCode();
  137.         }
  138.        
  139.        
  140.         protected override ConfigurationPropertyCollection Properties {
  141.             get { return _properties; }
  142.         }
  143.        
  144.         [ConfigurationProperty("name", IsRequired = true, IsKey = true, DefaultValue = "")]
  145.         public string Name {
  146.             get { return (string)base[_propName]; }
  147.             set { base[_propName] = value; }
  148.         }
  149.        
  150.         [ConfigurationProperty("serializeAs", IsRequired = true, DefaultValue = SettingsSerializeAs.String)]
  151.         public SettingsSerializeAs SerializeAs {
  152.             get { return (SettingsSerializeAs)base[_propSerializeAs]; }
  153.             set { base[_propSerializeAs] = value; }
  154.         }
  155.        
  156.         [ConfigurationProperty("value", IsRequired = true, DefaultValue = null)]
  157.         public SettingValueElement Value {
  158.             get { return (SettingValueElement)base[_propValue]; }
  159.             set { base[_propValue] = value; }
  160.         }
  161.     }
  162.    
  163.     public sealed class SettingValueElement : ConfigurationElement
  164.     {
  165.         private static ConfigurationPropertyCollection _properties;
  166.         private static XmlDocument doc = new XmlDocument();
  167.        
  168.         private XmlNode _valueXml;
  169.         private bool isModified = false;
  170.        
  171.         protected override ConfigurationPropertyCollection Properties {
  172.             get {
  173.                 if (_properties == null) {
  174.                     _properties = new ConfigurationPropertyCollection();
  175.                 }
  176.                
  177.                 return _properties;
  178.             }
  179.         }
  180.        
  181.         public XmlNode ValueXml {
  182.             get { return _valueXml; }
  183.             set {
  184.                 _valueXml = value;
  185.                 isModified = true;
  186.             }
  187.         }
  188.        
  189.         protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
  190.         {
  191.             ValueXml = doc.ReadNode(reader);
  192.         }
  193.        
  194.         public override bool Equals(object settingValue)
  195.         {
  196.             SettingValueElement u = settingValue as SettingValueElement;
  197.             return (u != null && Object.Equals(u.ValueXml, ValueXml));
  198.         }
  199.        
  200.         public override int GetHashCode()
  201.         {
  202.             return ValueXml.GetHashCode();
  203.         }
  204.        
  205.         protected override bool IsModified()
  206.         {
  207.             return isModified;
  208.         }
  209.        
  210.         protected override void ResetModified()
  211.         {
  212.             isModified = false;
  213.         }
  214.        
  215.         protected override bool SerializeToXmlElement(XmlWriter writer, string elementName)
  216.         {
  217.             if (ValueXml != null) {
  218.                 if (writer != null) {
  219.                     ValueXml.WriteTo(writer);
  220.                 }
  221.                 return true;
  222.             }
  223.            
  224.             return false;
  225.         }
  226.        
  227.         protected override void Reset(ConfigurationElement parentElement)
  228.         {
  229.             base.Reset(parentElement);
  230.             ValueXml = ((SettingValueElement)parentElement).ValueXml;
  231.         }
  232.        
  233.         protected override void Unmerge(ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
  234.         {
  235.             base.Unmerge(sourceElement, parentElement, saveMode);
  236.             ValueXml = ((SettingValueElement)sourceElement).ValueXml;
  237.         }
  238.     }
  239. }

Developer Fusion