xml序列化
Stream stream=new MemoryStream();
XmlSerializer ser = new XmlSerializer(t1.GetType());
ser.Serialize(stream, t1);
stream.Position = 0;
Test t2 = (Test)ser.Deserialize(stream);
Console.WriteLine(stream.ToString();
http://www.codeproject.com/csharp/cloneimpl_class.asp#xx377851xx
Hi,
just wondering why you don't use the .NET serialization for the purpose of cloning an object. The advantage is -IMHO- that you have more control of what should be cloned and what not (simply mark a field with the NonSerialized attribute).
example:
[Serializable]
class Foo: ICloneable
{
[NonSerialized]
public int TempVar;
public string Text;
public object Clone()
{
Stream stream=new MemoryStream();
try {
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, this);
stream.Position=0;
return bf.Deserialize(stream);
}
finally
{
stream.Close();
}
}
}
The Clone method is always the same and could therefore be placed in a utility class.
Just a thought...
浙公网安备 33010602011771号