clone - ShallowCopy with MemoryStream class c# -
i have generic methos deep copy. worried if may cause issues? 100% safe? if not, when can fail , should watch out for?
/// <summary> /// makes copy object. /// doesn't copy reference memory, data. /// </summary> /// <typeparam name="t">type of return object.</typeparam> /// <param name="item">object copied.</param> /// <returns>returns copied object.</returns> public static t clone<t>(this object item) { if (item != null) { binaryformatter formatter = new binaryformatter(); memorystream stream = new memorystream(); formatter.serialize(stream, item); stream.seek(0, seekorigin.begin); t result = (t)formatter.deserialize(stream); stream.close(); return result; } else return default(t); }
example:
customer customercopy = customer.clone<customer>();
Comments
Post a Comment