I\'m trying to use .MemberwiseClone()
on a custom class of mine, but it throws up this error:
Cannot access protected member \'object.Memberwise
you can use this litle extension for the classes that not implement ICloneable:
///
/// Clones a object via shallow copy
///
/// Object Type to Clone
/// Object to Clone
/// New Object reference
public static T CloneObject(this T obj) where T : class
{
if (obj == null) return null;
System.Reflection.MethodInfo inst = obj.GetType().GetMethod("MemberwiseClone",
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (inst != null)
return (T)inst.Invoke(obj, null);
else
return null;
}