title: Add properties at runtime
body: <p>I have a class which the programmer can use to dynamically add new properties. For that it implements the <code>ICustomTypeDescriptor</code> to be able to override <code>GetProperties()</code> method.</p>
  
  <pre><code>public class DynamicProperty
  {
      public object Value { get; set; }
  
      public Type Type { get; set; }
  
      public string Name { get; set; }
  
      public Collection&lt;Attribute&gt; Attributes { get; set; }
  }
  
  public class DynamicClass : ICustomTypeDescriptor
  {
      // Collection to code add dynamic properties
      public KeyedCollection&lt;string, DynamicProperty&gt; Properties
      {
          get;
          private set;
      }
  
      // ICustomTypeDescriptor implementation
      PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
      {
          // Properties founded within instance
          PropertyInfo[] instanceProps = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
  
          // Fill property collection with founded properties
          PropertyDescriptorCollection propsCollection = 
              new PropertyDescriptorCollection(instanceProps.Cast&lt;PropertyDescriptor&gt;().ToArray());
  
          // Fill property collection with dynamic properties (Properties)
          foreach (var prop in Properties)
          {
              // HOW TO?
          }
  
          return propsCollection;
      }
  }
  </code></pre>
  
  <p>Is it possible to iterate over the Properties list to add each property to <code>PropertyDescriptorCollection</code>?</p>
  
  <p>Basically I want the programmer to be able to add a <code>DynamicProperty</code> to a collection which will be handled by <code>GetProperties</code>. Something like:</p>
  
  <pre><code>new DynamicClass()
  {
      Properties = {
          new DynamicProperty() {
              Name = "StringProp",
              Type = System.String,
              Value = "My string here"
          },
  
          new DynamicProperty() {
              Name = "IntProp",
              Type = System.Int32,
              Value = 10
          }
      }
  }
  </code></pre>
  
  <p>Now those <code>Properties</code> would be setted to instance properties whenever <code>GetProperties</code>is called. Am I thinking this the right way?</p>