Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is generic code for configuration collection :</p> <pre><code>public class GenericConfigurationElementCollection&lt;T&gt; : ConfigurationElementCollection, IEnumerable&lt;T&gt; where T : ConfigurationElement, new() { List&lt;T&gt; _elements = new List&lt;T&gt;(); protected override ConfigurationElement CreateNewElement() { T newElement = new T(); _elements.Add(newElement); return newElement; } protected override object GetElementKey(ConfigurationElement element) { return _elements.Find(e =&gt; e.Equals(element)); } public new IEnumerator&lt;T&gt; GetEnumerator() { return _elements.GetEnumerator(); } } </code></pre> <p>After you have <code>GenericConfigurationElementCollection</code>, you can simple use it in the config section (this is an example from my Dispatcher):</p> <pre><code>public class DispatcherConfigurationSection: ConfigurationSection { [ConfigurationProperty("maxRetry", IsRequired = false, DefaultValue = 5)] public int MaxRetry { get { return (int)this["maxRetry"]; } set { this["maxRetry"] = value; } } [ConfigurationProperty("eventsDispatches", IsRequired = true)] [ConfigurationCollection(typeof(EventsDispatchConfigurationElement), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")] public GenericConfigurationElementCollection&lt;EventsDispatchConfigurationElement&gt; EventsDispatches { get { return (GenericConfigurationElementCollection&lt;EventsDispatchConfigurationElement&gt;)this["eventsDispatches"]; } } } </code></pre> <p>The Config Element is config Here:</p> <pre><code>public class EventsDispatchConfigurationElement : ConfigurationElement { [ConfigurationProperty("name", IsRequired = true)] public string Name { get { return (string) this["name"]; } set { this["name"] = value; } } } </code></pre> <p>The config file would look like this:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;dispatcherConfigurationSection&gt; &lt;eventsDispatches&gt; &lt;add name="Log" &gt;&lt;/add&gt; &lt;add name="Notification" &gt;&lt;/add&gt; &lt;add name="tester" &gt;&lt;/add&gt; &lt;/eventsDispatches&gt; &lt;/dispatcherConfigurationSection&gt; </code></pre> <p><em>Hope it help !</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload