title: Dynamically Add C# Properties at Runtime
body: <p>I know there are some questions that address this, but the answers usually follow along the lines of recommending a Dictionary or Collection of parameters, which doesn't work in my situation.</p>
  
  <p>I am using a library that works through reflection to do lots of clever things with objects with properties.  This works with defined classes, as well as dynamic classes.  I need to take this one step further and do something along these lines:</p>
  
  <pre><code>public static object GetDynamicObject(Dictionary&lt;string,object&gt; properties) {
      var myObject = new object();
      foreach (var property in properties) {
          //This next line obviously doesn't work... 
          myObject.AddProperty(property.Key,property.Value);
      }
      return myObject;
  }
  
  public void Main() {
      var properties = new Dictionary&lt;string,object&gt;();
      properties.Add("Property1",aCustomClassInstance);
      properties.Add("Property2","TestString2");
  
      var myObject = GetDynamicObject(properties);
  
      //Then use them like this (or rather the plug in uses them through reflection)
      var customClass = myObject.Property1;
      var myString = myObject.Property2;
  
  }
  </code></pre>
  
  <p>The library works fine with a dynamic variable type, with properties assigned manually.  However I don't know how many or what properties will be added beforehand.</p>