title: How to Convert between Interface derived types?
body: <p>In the most simplest for here is what I want to do:</p>
  
  <pre><code>interface InterfaceA
  {
      string var1 { get; set; }
      string var2 { get; set; }
  }
  
  public class DerivedA : InterfaceA
  {
      public string var1 { get; set; }
      public string var2 { get; set; }
  }
  
  public class DerivedB : InterfaceA
  {
      public string var1 { get; set; }
      public string var2 { get; set; }
  }
  
  public class DoEverything
  {
      InterfaceA A = new DerivedA();
      InterfaceA B = new DerivedB();
  
      A.var1 = "Test1";
      A.var2 = "Test2";
  
      B = ConvertObject&lt;DerivedB&gt;(A);
  
      write(B.var1 + ", " + B.var2);
  
      // This doesn't work, I would like it to though.
      public T ConvertObject&lt;T&gt;(object obj)
      {
          T result = (T)typeof(T).GetConstructor(new Type[] { }).Invoke(new object[] { });
  
          foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties())
          {
              if (propertyInfo.CanRead)
              {
                  try
                  {
                      object value = propertyInfo.GetValue(obj, null);
                      propertyInfo.SetValue(result, value, null);
                  }
                  catch { }
              }
          }
  
          return result;
      }
  }
  </code></pre>
  
  <p>Right now I have to manually convert each property in my objects and when a new property is added I have to remember to go the conversion methods and add it. I would much prefer that this be done automatically.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>thanks in advance</p>