The Labs \ Source Viewer \ SSCLI \ System.Diagnostics \ SwitchElementsCollection

  1. //------------------------------------------------------------------------------
  2. // <copyright file="SwitchElementsCollection.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. using System.Configuration;
  16. using System.Collections;
  17. using System.Collections.Specialized;
  18. namespace System.Diagnostics
  19. {
  20.     [ConfigurationCollection(typeof(SwitchElement))]
  21.     internal class SwitchElementsCollection : ConfigurationElementCollection
  22.     {
  23.        
  24.         public new SwitchElement this[string name]
  25.         {
  26.             get { return (SwitchElement)BaseGet(name); }
  27.         }
  28.        
  29.         public override ConfigurationElementCollectionType CollectionType {
  30.             get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
  31.         }
  32.        
  33.         protected override ConfigurationElement CreateNewElement()
  34.         {
  35.             return new SwitchElement();
  36.         }
  37.        
  38.         protected override object GetElementKey(ConfigurationElement element)
  39.         {
  40.             return ((SwitchElement)element).Name;
  41.         }
  42.     }
  43.    
  44.     internal class SwitchElement : ConfigurationElement
  45.     {
  46.         private static readonly ConfigurationPropertyCollection _properties;
  47.         private static readonly ConfigurationProperty _propName = new ConfigurationProperty("name", typeof(string), "", ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey);
  48.         private static readonly ConfigurationProperty _propValue = new ConfigurationProperty("value", typeof(string), null, ConfigurationPropertyOptions.IsRequired);
  49.        
  50.         private Hashtable _attributes;
  51.        
  52.         static SwitchElement()
  53.         {
  54.             _properties = new ConfigurationPropertyCollection();
  55.             _properties.Add(_propName);
  56.             _properties.Add(_propValue);
  57.         }
  58.        
  59.         public Hashtable Attributes {
  60.             get {
  61.                 if (_attributes == null)
  62.                     _attributes = new Hashtable(StringComparer.OrdinalIgnoreCase);
  63.                 return _attributes;
  64.             }
  65.         }
  66.        
  67.         [ConfigurationProperty("name", DefaultValue = "", IsRequired = true, IsKey = true)]
  68.         public string Name {
  69.             get { return (string)this[_propName]; }
  70.         }
  71.        
  72.         protected override ConfigurationPropertyCollection Properties {
  73.             get { return _properties; }
  74.         }
  75.        
  76.         [ConfigurationProperty("value", IsRequired = true)]
  77.         public string Value {
  78.             get { return (string)this[_propValue]; }
  79.         }
  80.        
  81.         protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
  82.         {
  83.             ConfigurationProperty _propDynamic = new ConfigurationProperty(name, typeof(string), value);
  84.             _properties.Add(_propDynamic);
  85.             base[_propDynamic] = value;
  86.             // Add them to the property bag
  87.             Attributes.Add(name, value);
  88.             return true;
  89.         }
  90.        
  91.         internal void ResetProperties()
  92.         {
  93.             // blow away any UnrecognizedAttributes that we have deserialized earlier
  94.             if (_attributes != null) {
  95.                 _attributes.Clear();
  96.                 _properties.Clear();
  97.                 _properties.Add(_propName);
  98.                 _properties.Add(_propValue);
  99.             }
  100.         }
  101.     }
  102. }

Developer Fusion