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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="SourceElementsCollection .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. using System.Xml;
  19. namespace System.Diagnostics
  20. {
  21.     [ConfigurationCollection(typeof(SourceElement), AddItemName = "source", CollectionType = ConfigurationElementCollectionType.BasicMap)]
  22.     internal class SourceElementsCollection : ConfigurationElementCollection
  23.     {
  24.        
  25.         public new SourceElement this[string name]
  26.         {
  27.             get { return (SourceElement)BaseGet(name); }
  28.         }
  29.        
  30.         protected override string ElementName {
  31.             get { return "source"; }
  32.         }
  33.        
  34.         public override ConfigurationElementCollectionType CollectionType {
  35.             get { return ConfigurationElementCollectionType.BasicMap; }
  36.         }
  37.        
  38.         protected override ConfigurationElement CreateNewElement()
  39.         {
  40.             SourceElement se = new SourceElement();
  41.             se.Listeners.InitializeDefaultInternal();
  42.             return se;
  43.         }
  44.        
  45.         protected override object GetElementKey(ConfigurationElement element)
  46.         {
  47.             return ((SourceElement)element).Name;
  48.         }
  49.     }
  50.    
  51.    
  52.     internal class SourceElement : ConfigurationElement
  53.     {
  54.         private static readonly ConfigurationPropertyCollection _properties;
  55.         private static readonly ConfigurationProperty _propName = new ConfigurationProperty("name", typeof(string), "", ConfigurationPropertyOptions.IsRequired);
  56.         private static readonly ConfigurationProperty _propSwitchName = new ConfigurationProperty("switchName", typeof(string), null, ConfigurationPropertyOptions.None);
  57.         private static readonly ConfigurationProperty _propSwitchValue = new ConfigurationProperty("switchValue", typeof(string), null, ConfigurationPropertyOptions.None);
  58.         private static readonly ConfigurationProperty _propSwitchType = new ConfigurationProperty("switchType", typeof(string), null, ConfigurationPropertyOptions.None);
  59.         private static readonly ConfigurationProperty _propListeners = new ConfigurationProperty("listeners", typeof(ListenerElementsCollection), new ListenerElementsCollection(), ConfigurationPropertyOptions.None);
  60.        
  61.         private Hashtable _attributes;
  62.        
  63.         static SourceElement()
  64.         {
  65.             _properties = new ConfigurationPropertyCollection();
  66.             _properties.Add(_propName);
  67.             _properties.Add(_propSwitchName);
  68.             _properties.Add(_propSwitchValue);
  69.             _properties.Add(_propSwitchType);
  70.             _properties.Add(_propListeners);
  71.         }
  72.        
  73.         public Hashtable Attributes {
  74.             get {
  75.                 if (_attributes == null)
  76.                     _attributes = new Hashtable(StringComparer.OrdinalIgnoreCase);
  77.                 return _attributes;
  78.             }
  79.         }
  80.        
  81.         [ConfigurationProperty("listeners")]
  82.         public ListenerElementsCollection Listeners {
  83.             get { return (ListenerElementsCollection)this[_propListeners]; }
  84.         }
  85.        
  86.         [ConfigurationProperty("name", IsRequired = true, DefaultValue = "")]
  87.         public string Name {
  88.             get { return (string)this[_propName]; }
  89.         }
  90.        
  91.         protected override ConfigurationPropertyCollection Properties {
  92.             get { return _properties; }
  93.         }
  94.        
  95.         [ConfigurationProperty("switchName")]
  96.         public string SwitchName {
  97.             get { return (string)this[_propSwitchName]; }
  98.         }
  99.        
  100.         [ConfigurationProperty("switchValue")]
  101.         public string SwitchValue {
  102.             get { return (string)this[_propSwitchValue]; }
  103.         }
  104.        
  105.         [ConfigurationProperty("switchType")]
  106.         public string SwitchType {
  107.             get { return (string)this[_propSwitchType]; }
  108.         }
  109.        
  110.        
  111.         protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
  112.         {
  113.             base.DeserializeElement(reader, serializeCollectionKey);
  114.            
  115.             if (!String.IsNullOrEmpty(SwitchName) && !String.IsNullOrEmpty(SwitchValue))
  116.                 throw new ConfigurationErrorsException(SR.GetString(SR.Only_specify_one, Name));
  117.         }
  118.        
  119.         protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
  120.         {
  121.             ConfigurationProperty _propDynamic = new ConfigurationProperty(name, typeof(string), value);
  122.             _properties.Add(_propDynamic);
  123.             base[_propDynamic] = value;
  124.             // Add them to the property bag
  125.             Attributes.Add(name, value);
  126.             return true;
  127.         }
  128.        
  129.         internal void ResetProperties()
  130.         {
  131.             // blow away any UnrecognizedAttributes that we have deserialized earlier
  132.             if (_attributes != null) {
  133.                 _attributes.Clear();
  134.                 _properties.Clear();
  135.                 _properties.Add(_propName);
  136.                 _properties.Add(_propSwitchName);
  137.                 _properties.Add(_propSwitchValue);
  138.                 _properties.Add(_propSwitchType);
  139.                 _properties.Add(_propListeners);
  140.             }
  141.         }
  142.     }
  143.    
  144. }

Developer Fusion