Hi,

I have been trying to fix some web service config stuff, and
noticed that ConfigurationElementCollection.ThrowOnDuplicate property
behavior is somewhat different from .net (the attached example
does not raise an error on .net while mono does).

According to the documentation, I believe that Mono behavior is
the correct behavior, but with this I find difficulty to fix
web service configuration issue, so I'm tempted to find that
current behavior is incorrect (and my understanding becomes wrong) ;-)

So, does anyone have ideas on how this property works?

Atsushi Eno
using System;
using System.Configuration;

public class MyElement : ConfigurationElement
{
        static ConfigurationProperty name = new ConfigurationProperty ("name",
                typeof (string), null, ConfigurationPropertyOptions.IsKey);
        static ConfigurationPropertyCollection props;

        static MyElement ()
        {
                props = new ConfigurationPropertyCollection ();
                props.Add (name);
        }
        
        public MyElement ()
        {
        }

        public MyElement (string name)
        {
                Name = name;
        }

        [ConfigurationProperty ("name", Options = 
ConfigurationPropertyOptions.IsKey)]
        public string Name {
                get { return (string) this [name]; }
                set { this [name] = value; }
        }

        protected override ConfigurationPropertyCollection Properties {
                get { return props; }
        }
}

[ConfigurationCollection (typeof (MyElement), CollectionType = 
ConfigurationElementCollectionType.AddRemoveClearMap)]
public class MyElementCollection : ConfigurationElementCollection
{
        protected override ConfigurationElement CreateNewElement ()
        {
                return new MyElement ();
        }
        protected override object GetElementKey (ConfigurationElement e)
        {
                return ((MyElement) e).Name;
        }

        public void Add (MyElement e)
        {
                BaseAdd (e);
        }

        protected override void BaseAdd (ConfigurationElement e)
        {
Console.WriteLine ("{0} {1}", BaseIndexOf (e), ThrowOnDuplicate);
                base.BaseAdd (e);
        }
}

public class MySection : ConfigurationSection
{
        MyElementCollection my_elems =
                new MyElementCollection ();

        [ConfigurationProperty ("MyElements")]
        public MyElementCollection MyElements {
                get { return my_elems; }
        }
}

public class Driver
{
        public static void Main ()
        {
                /*
                MyElementCollection c = new MyElementCollection ();
                MyElement e = new MyElement ("Foo");
                c.Add (e);
                c.Add (e);
                c.Add (new MyElement ("Foo"));
                */
                MySection ms = (MySection) ConfigurationManager.GetSection 
("MySection");
                foreach (MyElement e in ms.MyElements)
                        Console.WriteLine (e.Name);
        }
}

Attachment: throw-on-duplicate.exe.config
Description: application/xml

_______________________________________________
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list

Reply via email to