09 Clones everywhere

object.MemberwiseClone() can be a helpful method to copy an object without addressing every single property or field. But the problem is, it is protected.

To shallow-clone every object you got at hand, you need to make it somewhat more accessible:

public static class ObjectExtensions
{
  public static OT memberwiseClone<OT>(this OT obj)
  { return (OT)MemberwiseClone(obj); }
 
  static readonly Func<object, object> MemberwiseClone = makeMC();
 
  static Func<object, object> makeMC()
  {
    var m = typeof(object).GetMethod("MemberwiseClone",
      BindingFlags.NonPublic | BindingFlags.Instance);
    return (Func<object, object>)
      Delegate.CreateDelegate(typeof(Func<object, object>), m);
  }
}

With this extension method, every object may be cloned using instance.memberwiseClone(). Cloning system objects and especially objects that need to run a finalizer, is risky. So use this hack with care.

In optimum circumstances, MemberwiseClone is as fast as cloning by hand. It does not use Reflections.