Check out
public class CloneHelpers
at this url:
http://www.15seconds.com/issue/041124.htm
or
public class CloneHelpers
{
public static object DeepClone(object source)
{
MemoryStream m = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(m, source);
m.Position = 0;
return b.Deserialize(m);
}
public static bool DeepEquals(object objA,object objB)
{
MemoryStream serA = serializedStream(objA);
MemoryStream serB = serializedStream(objB);
if(serA.Length!=serA.Length)
return false;
while(serA.Position<serA.Length)
{
if(serA.ReadByte()!=serB.ReadByte())
return false;
}
return true;
}
public static MemoryStream serializedStream(object source)
{
MemoryStream m = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(m, source);
m.Position = 0;
return m;
}
}
"Salvador" <Sa******@discussions.microsoft.comwrote in message
news:9D**********************************@microsof t.com...
Hi,
I want to do a very simple thing that I use to do it in C++.
I have an instance of a class and I have a function that returns a copy of
that instance (a new object).
Because a class is a reference type in .NET if I say retun MyInstance
actually returns a reference to my original instance. In this scenario I
want
to pass a copy of it.
IFoo MyInstanceOfFoo = new Foo();
IFoo GetAuction(int id)
{
return MyInstaceOfFoo; // This returns the instance
}
Now, do I have a quick way of returning a copy? (Without me creating a new
foo and copy every single property?)
Regards
--
Salvador