2010-01-09 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Configuration / System.Configuration / ConfigurationElementCollection.cs
index 1627a0b82871edc3a2421dc41d76a119aeb6b870..db98f4c4067d25ffe4617febe9cbdcfd4d3c0504 100644 (file)
@@ -66,8 +66,10 @@ namespace System.Configuration
                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;
@@ -120,7 +122,13 @@ namespace System.Configuration
                }
 
                protected virtual bool ThrowOnDuplicate {
-                       get { return true; }
+                       get {
+                               if (CollectionType != ConfigurationElementCollectionType.AddRemoveClearMap &&
+                                   CollectionType != ConfigurationElementCollectionType.AddRemoveClearMapAlternate)
+                                       return false;
+                               
+                               return true;
+                       }
                }
                
                protected internal string AddElementName {
@@ -149,21 +157,25 @@ namespace System.Configuration
 
                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;
                }