title: GetKey in ConfigurationElementCollection from ConfigurationPropertyAttribute?
body: <p>There are a lot of examples out there that explain how to create your own ConfigurationElementCollection, for instance: <a href="http://How%20to%20implement%20a%20ConfigurationSection%20with%20a%20ConfigurationElementCollection" rel="nofollow">Stackoverflow: How to implement your own ConfigurationElementCollection?</a></p>
  
  <p>One of the functions you'll have to override is GetElementKey:</p>
  
  <pre><code>protected override object GetElementKey(ConfigurationElement element)
  {
    return ((ServiceConfig) element).Port;
  }
  </code></pre>
  
  <p>where property Port is defined as follows:</p>
  
  <pre><code>[ConfigurationProperty("Port", IsRequired = true, IsKey = true)]
  public int Port 
  {
    get { return (int) this["Port"]; }
    set { this["Port"] = value; }
  }
  </code></pre>
  
  <p>My configuration has several ConfigurationElementCollections that look very similar. The GetElementKey function is the only function that inhibits the use of a generic ConfigurationElementCollection because of the identifier of the key. The ConfigurationPropertyAttribute already informs me which property is the key.</p>
  
  <p><strong>Is it possible to get the Key property via the ConfigurationPropertyAttribute?</strong> </p>
  
  <p>Code would be like:</p>
  
  <pre><code>public class ConfigCollection&lt;T&gt; : ConfigurationElementCollection where T: ConfigurationElement, new()
  {
      protected override Object GetElementKey(ConfigurationElement element)
      {
          // get the propertyInfo of property that has IsKey = true
          PropertyInfo keyPropertyInfo = ... 
          object keyValue = keyPropertyInfo.GetValue(element);
          return keyValue;
       }
  </code></pre>