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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="ConnectionStringSettingsCollection.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.     // class ConnectionStringsSection
  26.    
  27.     [ConfigurationCollection(typeof(ConnectionStringSettings))]
  28.     public sealed class ConnectionStringSettingsCollection : ConfigurationElementCollection
  29.     {
  30.         private static ConfigurationPropertyCollection _properties;
  31.        
  32.         static ConnectionStringSettingsCollection()
  33.         {
  34.             // Property initialization
  35.             _properties = new ConfigurationPropertyCollection();
  36.         }
  37.        
  38.         protected internal override ConfigurationPropertyCollection Properties {
  39.             get { return _properties; }
  40.         }
  41.        
  42.         public ConnectionStringSettingsCollection() : base(StringComparer.OrdinalIgnoreCase)
  43.         {
  44.         }
  45.         public ConnectionStringSettings this[int index]
  46.         {
  47.             get { return (ConnectionStringSettings)BaseGet(index); }
  48.             set {
  49.                 if (BaseGet(index) != null) {
  50.                     BaseRemoveAt(index);
  51.                 }
  52.                 BaseAdd(index, value);
  53.             }
  54.         }
  55.        
  56.         public new ConnectionStringSettings this[string name]
  57.         {
  58.             get { return (ConnectionStringSettings)BaseGet(name); }
  59.         }
  60.        
  61.         public int IndexOf(ConnectionStringSettings settings)
  62.         {
  63.             return BaseIndexOf(settings);
  64.         }
  65.        
  66.         // the connection string behavior is strange in that is acts kind of like a
  67.         // basic map and partially like a add remove clear collection
  68.         // Overriding these methods allows for the specific behaviors to be
  69.         // patterened
  70.         protected override void BaseAdd(int index, ConfigurationElement element)
  71.         {
  72.             if (index == -1) {
  73.                 BaseAdd(element, false);
  74.             }
  75.             else {
  76.                 base.BaseAdd(index, element);
  77.             }
  78.         }
  79.        
  80.         public void Add(ConnectionStringSettings settings)
  81.         {
  82.             BaseAdd(settings);
  83.         }
  84.        
  85.         public void Remove(ConnectionStringSettings settings)
  86.         {
  87.             if (BaseIndexOf(settings) >= 0) {
  88.                 BaseRemove(settings.Key);
  89.             }
  90.         }
  91.        
  92.         public void RemoveAt(int index)
  93.         {
  94.             BaseRemoveAt(index);
  95.         }
  96.        
  97.         public void Remove(string name)
  98.         {
  99.             BaseRemove(name);
  100.         }
  101.        
  102.         protected override ConfigurationElement CreateNewElement()
  103.         {
  104.             return new ConnectionStringSettings();
  105.         }
  106.        
  107.         protected override object GetElementKey(ConfigurationElement element)
  108.         {
  109.             return ((ConnectionStringSettings)element).Key;
  110.         }
  111.        
  112.         public void Clear()
  113.         {
  114.             BaseClear();
  115.         }
  116.     }
  117. }

Developer Fusion