c# - Custom Configuration Section - Remove unwanted collection tags -
c# - Custom Configuration Section - Remove unwanted collection tags -
i have created own set of classes custom nested configuration collection in app.config. below can see current configuration must use. want know how can modify classes don't need autosyncconfiguration , watchedfolders elements. i'd resulting configuration section this:
<custom> <backuplocation name="s3" details="accesskey=asdf;secretkey=asdf;bucketname=asdf"> <watchedfolder name="test1" localfolder="c" remotefolder="z" filespec="*"></watchedfolder> <watchedfolder name="test2" localfolder="d" remotefolder="x" filespec="*.doc"></watchedfolder> </backuplocation> <backuplocation name="external" details="mappeddrive=x;"> <watchedfolder name="test" localfolder="d" remotefolder="xphotos" filespec="*.jpeg"></watchedfolder> </backuplocation> </custom>
below classes, relevant portion of app.config , line of code grab custom configuration:
public class custom : configurationsection { [configurationproperty("autosyncconfiguration")] public backuplocationelementcollection autosyncconfiguration { { homecoming this["autosyncconfiguration"] backuplocationelementcollection; } } } public class backuplocationelementcollection : configurationelementcollection { protected override configurationelement createnewelement() { homecoming new backuplocationelement(); } protected override object getelementkey(configurationelement element) { homecoming ((backuplocationelement)element).name; } public override configurationelementcollectiontype collectiontype { { homecoming configurationelementcollectiontype.basicmap; } } protected override string elementname { { homecoming "backuplocation"; } } public backuplocationelement this[int index] { { homecoming (backuplocationelement)baseget(index); } set { if (baseget(index) != null) { baseremoveat(index); } baseadd(index, value); } } new public backuplocationelement this[string backupname] { { homecoming (backuplocationelement)baseget(backupname); } } public bool containskey(string key) { bool result = false; object[] keys = basegetallkeys(); foreach (object obj in keys) { if ((string)obj == key) { result = true; break; } } homecoming result; } } public class backuplocationelement : configurationelement { [configurationproperty("name", isrequired = true, iskey = true)] public string name { { homecoming this["name"] string; } set { this["name"] = value; } } [configurationproperty("details", isrequired = true, iskey = false)] public string details { { homecoming this["details"] string; } set { this["details"] = value; } } [configurationproperty("watchedfolders")] public watchedfolderelementcollection watchedfolders { { homecoming this["watchedfolders"] watchedfolderelementcollection; } } } public class watchedfolderelementcollection : configurationelementcollection { protected override configurationelement createnewelement() { homecoming new watchedfolderelement(); } protected override object getelementkey(configurationelement element) { homecoming ((watchedfolderelement)element).name; } public override configurationelementcollectiontype collectiontype { { homecoming configurationelementcollectiontype.basicmap; } } protected override string elementname { { homecoming "watchedfolder"; } } public watchedfolderelement this[int index] { { homecoming (watchedfolderelement)baseget(index); } set { if (baseget(index) != null) { baseremoveat(index); } baseadd(index, value); } } new public watchedfolderelement this[string foldername] { { homecoming (watchedfolderelement)baseget(foldername); } } public bool containskey(string key) { bool result = false; object[] keys = basegetallkeys(); foreach (object obj in keys) { if ((string)obj == key) { result = true; break; } } homecoming result; } } public class watchedfolderelement : configurationelement { [configurationproperty("name", isrequired = true, iskey = true)] public string name { { homecoming this["name"] string; } set { this["name"] = value; } } [configurationproperty("localfolder", isrequired = true, iskey = false)] public string localfolder { { homecoming this["localfolder"] string; } set { this["localfolder"] = value; } } [configurationproperty("remotefolder", isrequired = true, iskey = false)] public string remotefolder { { homecoming this["remotefolder"] string; } set { this["remotefolder"] = value; } } [configurationproperty("filespec", isrequired = true, iskey = false)] public string filespec { { homecoming this["filespec"] string; } set { this["filespec"] = value; } } }
the next app.config:
<configuration> <configsections> <section name="custom" type="autosync.custom, autosync" /> </configsections> <custom> <autosyncconfiguration> <backuplocation name="s3" details="accesskey=asdf;secretkey=asdf;bucketname=asdf"> <watchedfolders> <watchedfolder name="test1" localfolder="c" remotefolder="z" filespec="*"/> </watchedfolders> </backuplocation> <backuplocation name="external" details="mappeddrive=x;"> <watchedfolders> <watchedfolder name="test" localfolder="d" remotefolder="xphotos" filespec="*.jpeg" /> </watchedfolders> </backuplocation> </autosyncconfiguration> </custom> </configuration>
and code follows:
custom config = (custom)configurationmanager.getsection("custom");
can tell me how collapse configuration section rid of "unused" elements?
i don't see how can done adding one-two lines of code, since how .net configuration serializing/deserializing collections. 1 solution - create own type converter - descendent of configurationconverterbase
. apply autosyncconfiguration
property. in converter whatever want serialize/deserialize collections.
c# .net configuration
Comments
Post a Comment