title: How to extend class with an extra property
body: <p>Suppose I've got a class named <code>Foo</code>. </p>
  
  <p>I cannot change the <code>Foo</code> class but I wan't to extend it with a property named <code>Bar</code> of type <code>string</code>. </p>
  
  <p>Also I've got a lot more classes like <code>Foo</code> so I'm interested in a 'generic' solution.</p>
  
  <p>I'm looking into <code>ExpandoObject</code>, <code>dynamic</code> and it gives me the result I'm asking for but I was wondering it it could be done without using <code>dynamic</code>...</p>
  
  <pre><code>static void Main(string[] args)
  {
      var foo = new Foo() { Thing = "this" };
      var fooplus = Merge(foo, new { Bar = " and that" });
      Console.Write(string.Concat(fooplus.Thing, fooplus.Bar));
      Console.ReadKey();
  }
  
  public class Foo
  {
      public string Thing { get; set; }
  }
  
  public static dynamic Merge(object item1, object item2)
  {
      if (item1 == null || item2 == null)
      return item1 ?? item2 ?? new ExpandoObject();
  
      dynamic expando = new ExpandoObject();
      var result = expando as IDictionary&lt;string, object&gt;;
      foreach (System.Reflection.PropertyInfo fi in item1.GetType().GetProperties())
      {
          result[fi.Name] = fi.GetValue(item1, null);
      }
      foreach (System.Reflection.PropertyInfo fi in item2.GetType().GetProperties())
      {
          result[fi.Name] = fi.GetValue(item2, null);
      }
      return result;
  }
  </code></pre>