1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
//------------------------------------------------------------------------------
// <copyright file="KeyValueConfigurationCollection.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Configuration {
using System;
using System.Xml;
using System.Configuration;
using System.Collections.Specialized;
using System.Collections;
using System.IO;
using System.Text;
[ConfigurationCollection(typeof(KeyValueConfigurationElement))]
public class KeyValueConfigurationCollection : ConfigurationElementCollection {
private static ConfigurationPropertyCollection _properties;
static KeyValueConfigurationCollection() {
// Property initialization
_properties = new ConfigurationPropertyCollection();
}
protected internal override ConfigurationPropertyCollection Properties {
get {
return _properties;
}
}
//
// Constructor
//
public KeyValueConfigurationCollection() :
base(StringComparer.OrdinalIgnoreCase) {
internalAddToEnd = true;
}
//
// Accessors
//
protected override bool ThrowOnDuplicate {
get {
return false;
}
}
public new KeyValueConfigurationElement this[string key] {
get {
return (KeyValueConfigurationElement)BaseGet(key);
}
#if DONT_COMPILE
// if we ever expose this element this will be handy
set {
int index = -1; // append by default
KeyValueConfigurationElement tempElement = (KeyValueConfigurationElement)BaseGet(key);
if (tempElement != null) {
index = BaseIndexOf(tempElement);
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
#endif
}
public String[] AllKeys {
get {
return StringUtil.ObjectArrayToStringArray(BaseGetAllKeys());
}
}
//
// Methods
//
public void Add(KeyValueConfigurationElement keyValue) {
// Need to initialize in order to get the key
keyValue.Init();
// the appsettings add works more like a namevalue collection add in that it appends values
// when add is called and teh key already exists.
KeyValueConfigurationElement oldValue = (KeyValueConfigurationElement)BaseGet(keyValue.Key);
if (oldValue == null) {
BaseAdd(keyValue);
}
else {
oldValue.Value += "," + keyValue.Value;
int index = BaseIndexOf(oldValue);
BaseRemoveAt(index);
BaseAdd(index, oldValue);
}
}
public void Add(String key, String value) {
KeyValueConfigurationElement element = new KeyValueConfigurationElement(key, value);
Add(element);
}
#if DONT_COMPILE
public void Remove(KeyValueConfigurationElement keyValue)
{
if (BaseIndexOf(keyValue) >= 0)
BaseRemove(keyValue.Key);
}
#endif
public void Remove(string key) {
BaseRemove(key);
}
public void Clear() {
BaseClear();
}
protected override ConfigurationElement CreateNewElement() {
return new KeyValueConfigurationElement();
}
protected override object GetElementKey(ConfigurationElement element) {
return ((KeyValueConfigurationElement)element).Key;
}
}
}
|