none
How to make set property of the ConfigurationElement? RRS feed

  • Question

  • I made codes for my ConfigurationElement of the ConfigurationElementCollection.

    However I can't make codes for set propery of the MyDeviceConfigElement.

    Can anybody make set property for me?

    [ConfigurationCollection(typeof(MyDeviceConfigElement), AddItemName = "device", CollectionType = ConfigurationElementCollectionType.BasicMap)] public class MyDeviceConfigElementCollection : ConfigurationElementCollection { . . public MyDeviceConfigElement this[string deviceName] { get { return (MyDeviceConfigElement)base.BaseGet(deviceName); }

    } . . }


    Saturday, November 24, 2018 5:19 PM

Answers

  • Hi Jeff0803,

    Thank you for posting here.

    For your question, please try the code below. Please pay attention to the bold code.

    [ConfigurationCollection(typeof(MyDeviceConfigElementCollection), AddItemName = "device", CollectionType = ConfigurationElementCollectionType.BasicMap)]
        public  class MyDeviceConfigElementCollection:ConfigurationElementCollection
        {
            public MyDeviceConfigElementCollection myDevices
            {
                get
                {
                    MyDeviceConfigElementCollection mydevice =
                        (MyDeviceConfigElementCollection)base["device"];
    
                    return mydevice;
                }
    
                set
                {
                    MyDeviceConfigElementCollection  myDevice= value;
                }
            }
    
            public MyDeviceConfigElementCollection()
            {
                MyDeviceConfigElement myDevice = new MyDeviceConfigElement();
                myDevices.BaseAdd(myDevice);
            }
            protected override ConfigurationElement CreateNewElement()
            {
                return new MyDeviceConfigElement();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((MyDeviceConfigElement)element).Name;
            }
           new public MyDeviceConfigElement this[string devicename]
            {
                get
                {
                    return (MyDeviceConfigElement)BaseGet(devicename);
                }
                set
                {
                    if(BaseGet(devicename)!=null)
                    {
                        BaseRemove(devicename);
                    }
                    BaseAdd(value);
                }
            }
         }
        public class MyDeviceConfigElement:ConfigurationElement
        {
            public MyDeviceConfigElement(string name)
            {
                this.Name = name;
            }
            public MyDeviceConfigElement()
            {
    
            }
            public string Name
            {
                get
                {
                    return (string)this["name"];
                }
                set
                {
                    this["name"] = value;
                }
            }
    
        }
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jeff0803 Wednesday, November 28, 2018 2:05 AM
    Monday, November 26, 2018 7:48 AM
    Moderator