Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to have custom attribute in ConfigurationElementCollection?

for configuration as following

<MyCollection default="one">   <entry name="one" ... other attrubutes />   ... other entries </MyCollection> 

when implement a MyCollection, what should i do for the "default" attribute?

like image 575
jojo Avatar asked Jan 12 '12 02:01

jojo


2 Answers

Let's suppose you have this .config file:

<configuration>     <configSections>         <section name="mySection" type="ConsoleApplication1.MySection, ConsoleApplication1" /> // update type  & assembly names accordingly     </configSections>      <mySection>         <MyCollection default="one">             <entry name="one" />             <entry name="two" />         </MyCollection>     </mySection> </configuration> 

Then, with this code:

public class MySection : ConfigurationSection {     [ConfigurationProperty("MyCollection", Options = ConfigurationPropertyOptions.IsRequired)]     public MyCollection MyCollection     {         get         {             return (MyCollection)this["MyCollection"];         }     } }  [ConfigurationCollection(typeof(EntryElement), AddItemName = "entry", CollectionType = ConfigurationElementCollectionType.BasicMap)] public class MyCollection : ConfigurationElementCollection {     protected override ConfigurationElement CreateNewElement()     {         return new EntryElement();     }      protected override object GetElementKey(ConfigurationElement element)     {         if (element == null)             throw new ArgumentNullException("element");          return ((EntryElement)element).Name;     }      [ConfigurationProperty("default", IsRequired = false)]     public string Default     {         get         {             return (string)base["default"];         }     } }  public class EntryElement : ConfigurationElement {     [ConfigurationProperty("name", IsRequired = true, IsKey = true)]     public string Name     {         get         {             return (string)base["name"];         }     } } 

you can read the configuration with the 'default' attribute, like this:

    MySection section = (MySection)ConfigurationManager.GetSection("mySection");     Console.WriteLine(section.MyCollection.Default); 

This will output "one"

like image 81
Simon Mourier Avatar answered Sep 16 '22 17:09

Simon Mourier


I don't know if it's possible to have a default value in a ConfigurationElementCollection. (it doesn't seen to have any property for default value).

I guess you have to implement this by yourself. Look at the example below.

public class Repository : ConfigurationElement {     [ConfigurationProperty("key", IsRequired = true)]     public string Key     {         get { return (string)this["key"]; }     }      [ConfigurationProperty("value", IsRequired = true)]     public string Value     {         get { return (string)this["value"]; }     } }  public class RepositoryCollection : ConfigurationElementCollection {     protected override ConfigurationElement CreateNewElement()     {         return new Repository();     }      protected override object GetElementKey(ConfigurationElement element)     {         return (element as Repository).Key;     }      public Repository this[int index]     {         get { return base.BaseGet(index) as Repository; }     }      public new Repository this[string key]     {         get { return base.BaseGet(key) as Repository; }     }  }  public class MyConfig : ConfigurationSection {     [ConfigurationProperty("currentRepository", IsRequired = true)]     private string InternalCurrentRepository     {         get { return (string)this["currentRepository"]; }     }      [ConfigurationProperty("repositories", IsRequired = true)]     private RepositoryCollection InternalRepositories     {         get { return this["repositories"] as RepositoryCollection; }     } } 

Here's the XML config:

  <myConfig currentRepository="SQL2008">     <repositories>       <add key="SQL2008" value="abc"/>       <add key="Oracle" value="xyz"/>     </repositories>   </myConfig> 

And then, at your code, you access the default item using the following:

MyConfig conf = (MyConfig)ConfigurationManager.GetSection("myConfig"); string myValue = conf.Repositories[conf.CurrentRepository].Value; 

Of course, the MyConfig class can hide the details of accessing the Repositories and CurrentRepository properties. You can have a property called DefaultRepository (of type Repository) in MyConfig class to return this.

like image 36
Fabio Avatar answered Sep 18 '22 17:09

Fabio