namespace System.Configuration
{
[DebuggerDisplayAttribute ("Count = {Count}")]
- public abstract class ConfigurationElementCollection : ConfigurationElement, ICollection, IEnumerable
+ public abstract partial class ConfigurationElementCollection : ConfigurationElement, ICollection, IEnumerable
{
ArrayList list = new ArrayList ();
ArrayList removed;
internal override void InitFromProperty (PropertyInformation propertyInfo)
{
ConfigurationCollectionAttribute colat = propertyInfo.Property.CollectionAttribute;
+
if (colat == null)
- colat = ElementMap.GetMap (GetType ()).CollectionAttribute;
+ colat = Attribute.GetCustomAttribute (propertyInfo.Type, typeof (ConfigurationCollectionAttribute)) as ConfigurationCollectionAttribute;
+
if (colat != null) {
addElementName = colat.AddItemName;
clearElementName = colat.ClearItemsName;
}
protected virtual bool ThrowOnDuplicate {
- get { return true; }
+ get {
+ if (CollectionType != ConfigurationElementCollectionType.AddRemoveClearMap &&
+ CollectionType != ConfigurationElementCollectionType.AddRemoveClearMapAlternate)
+ return false;
+
+ return true;
+ }
}
protected internal string AddElementName {
protected void BaseAdd (ConfigurationElement element, bool throwIfExists)
{
- if (throwIfExists && BaseIndexOf (element) != -1)
- throw new ConfigurationException ("Duplicate element in collection");
if (IsReadOnly ())
throw new ConfigurationErrorsException ("Collection is read only.");
- int old_index = IndexOfKey (GetElementKey (element));
if (IsAlternate) {
list.Insert (inheritedLimitIndex, element);
inheritedLimitIndex++;
}
else {
+ int old_index = IndexOfKey (GetElementKey (element));
+ if (old_index >= 0) {
+ if (element.Equals (list [old_index]))
+ return;
+ if (throwIfExists)
+ throw new ConfigurationException ("Duplicate element in collection");
+ list.RemoveAt (old_index);
+ }
list.Add (element);
}
- if (!IsAlternate && old_index != -1)
- list.RemoveAt (old_index);
+
modified = true;
}
}
else if (elementName == removeElementName) {
ConfigurationElement elem = CreateNewElementInternal (null);
- ConfigurationRemoveElement removeElem = new ConfigurationRemoveElement (elem);
+ ConfigurationRemoveElement removeElem = new ConfigurationRemoveElement (elem, this);
removeElem.DeserializeElement (reader, true);
BaseRemove (removeElem.KeyValue);
modified = false;