title: C# reflection, cloning
body: <p>Say I have this class Myclass that contains this method:</p>
  
  <pre><code> public class MyClass
      {
          public int MyProperty { get; set; }
  
          public int MySecondProperty { get; set; }
  
          public MyOtherClass subClass { get; set; }
  
          public object clone&lt;T&gt;(object original, T emptyObj)
          {
  
              FieldInfo[] fis = this.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  
  
              object tempMyClass = Activator.CreateInstance(typeof(T));
  
  
              foreach (FieldInfo fi in fis)
              {
                  if (fi.FieldType.Namespace != original.GetType().Namespace)
                      fi.SetValue(tempMyClass, fi.GetValue(original));
                  else
                      fi.SetValue(tempMyClass, this.clone(fi.GetValue(original), fi.GetValue(original)));
              }
  
              return tempMyClass;
          }
  }
  </code></pre>
  
  <p>Then this class:</p>
  
  <pre><code>public class MyOtherClass 
  {
      public int MyProperty777 { get; set; }
  }
  </code></pre>
  
  <p>when I do this:</p>
  
  <pre><code>MyClass a = new MyClass { 
                          MyProperty = 1, 
                          MySecondProperty = 2, 
                          subClass = new MyOtherClass() { MyProperty777 = -1 } 
                          };
              MyClass b = a.clone(a, a) as MyClass;
  </code></pre>
  
  <p>how come on the second call to clone, T is of type object and not of type MyOtherClass</p>