Wednesday, September 21, 2005
Saturday, September 17, 2005
Tuesday, September 13, 2005
Monday, September 12, 2005
Friday, September 09, 2005
Tuesday, September 06, 2005
Monday, September 05, 2005
Saturday, September 03, 2005
clone object in dotnet
private static void TestClone()
{
Person p1 = new Person();
p1.Age = 26;
p1.UserName = "yyanghhong";
Person p2 = (Person)CloneObjectEx(p1);
p2.UserName = "unruledboy";
Console.WriteLine(p1.UserName);
Console.WriteLine(p2.UserName);
}
public static Person CloneObject(Person ObjectInstance)
{
BinaryFormatter bFormatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
bFormatter.Serialize(stream, ObjectInstance);
stream.Seek(0, SeekOrigin.Begin);
Person newObject = (Person)bFormatter.Deserialize(stream);
return newObject;
}
public static object CloneObjectEx(object ObjectInstance)
{
BinaryFormatter bFormatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
bFormatter.Serialize(stream, ObjectInstance);
stream.Seek(0, SeekOrigin.Begin);
return bFormatter.Deserialize(stream);
}
[Serializable]
public class Person
{
private int age;
private string userName;
public Person()
{
}
public int Age
{
get{return age;}
set{age = value;}
}
public string UserName
{
get{return userName;}
set{userName = value;}
}
}
{
Person p1 = new Person();
p1.Age = 26;
p1.UserName = "yyanghhong";
Person p2 = (Person)CloneObjectEx(p1);
p2.UserName = "unruledboy";
Console.WriteLine(p1.UserName);
Console.WriteLine(p2.UserName);
}
public static Person CloneObject(Person ObjectInstance)
{
BinaryFormatter bFormatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
bFormatter.Serialize(stream, ObjectInstance);
stream.Seek(0, SeekOrigin.Begin);
Person newObject = (Person)bFormatter.Deserialize(stream);
return newObject;
}
public static object CloneObjectEx(object ObjectInstance)
{
BinaryFormatter bFormatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
bFormatter.Serialize(stream, ObjectInstance);
stream.Seek(0, SeekOrigin.Begin);
return bFormatter.Deserialize(stream);
}
[Serializable]
public class Person
{
private int age;
private string userName;
public Person()
{
}
public int Age
{
get{return age;}
set{age = value;}
}
public string UserName
{
get{return userName;}
set{userName = value;}
}
}
Friday, September 02, 2005
Subscribe to:
Posts (Atom)