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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="KeyValueConfigurationElement.cs" company="Microsoft">
  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.     public class KeyValueConfigurationElement : ConfigurationElement
  26.     {
  27.         private static ConfigurationPropertyCollection _properties;
  28.         private static readonly ConfigurationProperty _propKey = new ConfigurationProperty("key", typeof(string), String.Empty, ConfigurationPropertyOptions.IsKey | ConfigurationPropertyOptions.IsRequired);
  29.         private static readonly ConfigurationProperty _propValue = new ConfigurationProperty("value", typeof(string), String.Empty, ConfigurationPropertyOptions.None);
  30.        
  31.         static KeyValueConfigurationElement()
  32.         {
  33.             // Property initialization
  34.             _properties = new ConfigurationPropertyCollection();
  35.             _properties.Add(_propKey);
  36.             _properties.Add(_propValue);
  37.         }
  38.        
  39.         protected internal override ConfigurationPropertyCollection Properties {
  40.             get { return _properties; }
  41.         }
  42.        
  43.         bool _needsInit;
  44.         string _initKey;
  45.         string _initValue;
  46.        
  47.         //
  48.         // Constructor
  49.         //
  50.         internal KeyValueConfigurationElement()
  51.         {
  52.         }
  53.        
  54.         public KeyValueConfigurationElement(string key, string value)
  55.         {
  56.             _needsInit = true;
  57.             _initKey = key;
  58.             _initValue = value;
  59.         }
  60.        
  61.         protected internal override void Init()
  62.         {
  63.             base.Init();
  64.            
  65.             // We cannot initialize configuration properties in the constructor,
  66.             // because Properties is an overridable virtual property that
  67.             // hence may not be available in the constructor.
  68.             if (_needsInit) {
  69.                 _needsInit = false;
  70.                 base[_propKey] = _initKey;
  71.                 Value = _initValue;
  72.             }
  73.         }
  74.        
  75.         //
  76.         // Properties
  77.         //
  78.        
  79.         [ConfigurationProperty("key", Options = ConfigurationPropertyOptions.IsKey, DefaultValue = "")]
  80.         public string Key {
  81.             get { return (string)base[_propKey]; }
  82.         }
  83.        
  84.         [ConfigurationProperty("value", DefaultValue = "")]
  85.         public string Value {
  86.             get { return (string)base[_propValue]; }
  87.             set { base[_propValue] = value; }
  88.         }
  89.     }
  90. }

Developer Fusion