.net - nameValueCollection inside an element of a configurationElementCollection -
how can use namevaluecollection inside element of configurationelementcollection?
this:
<connectionssection> <vendors> <add name="nhonho" username="name" password="password"> <add key="somekey" value="somevalue" /> </add> </vendors> </connectionssection>
i got far creating configuration element 'vendors'.
public class connections : configurationsection { private static string sectionname = "connectionssection"; private static connections _section; public static connections section { { return _section ?? (_section = (connections)configurationmanager.getsection(sectionname)); } } [configurationproperty("vendors")] public vendorcollection vendors { { return base["vendors"] vendorcollection; } } } public class vendorcollection : configurationelementcollection { private ilist<vendorelement> _vendors = new list<vendorelement>(); public override configurationelementcollectiontype collectiontype { { return configurationelementcollectiontype.addremoveclearmap; } } public vendorelement get(string proname) { return baseget(proname) vendorelement; } public void add(vendorelement element) { baseadd(element); } public void clear() { baseclear(); } public void remove(vendorelement element) { baseremove(element.name); } public void remove(string name) { baseremove(name); } public void removeat(int index) { baseremoveat(index); } protected override configurationelement createnewelement() { return new vendorelement(); } protected override object getelementkey(configurationelement element) { return ((vendorelement)element).name; } } public class vendorelement : configurationelement { [configurationproperty("name")] public string name { { return base["name"] string; } } [configurationproperty("username")] public string username { { return base["username"] string; } } [configurationproperty("password")] public string password { { return base["password"] string; } } [configurationproperty("", isdefaultcollection= true)] public namevaluecollection extensions { { return base[""] namevaluecollection; } } }
i had found, mistake, link bitbucket project resolved problem.
/// <summary> /// hold name value collection - allow duplication of name , values /// sample : /// <something> /// <add name="name1" value="value1" /> /// <add name="name1" value="value1" /> /// </something> /// </summary> public class namevaluecollection : configurationelementcollection { protected override configurationelement createnewelement() { return new namevalueelement(); } protected override object getelementkey(configurationelement element) { //to allow duplication of value return element.elementinformation.linenumber; } } /// <summary> /// value element /// </summary> public class namevalueelement : configurationelement { [configurationproperty("name")] public string name { { return (string)this["name"]; } set { this["name"] = value; } } [configurationproperty("value")] public string value { { return (string)this["value"]; } set { this["value"] = value; } } }
src: https://bitbucket.org/kkurni/custom-configuration-element/src/13997e83b899/namevaluecollection.cs
Comments
Post a Comment