Copying an object

  • Thread starter Thread starter Matias Hernandez
  • Start date Start date
M

Matias Hernandez

Hello there! I'm fairly new to C#, so please excuse me if this
question seems somewhat basic. I need a way to copy the fields from
one object into another. It's akin to the Clone() method, however in
this case the destination object already exists and I cannot replace
it with another instance. Something like "public void CloneInto(Object
copy);".

I suppose this can be done manually by using Reflection, but maybe
this problem is already solved in .NET and I just haven't found that
solution...

Best regards,
- Matias
 
How about doing something like this;

public myObject CloneInto(Object copy)
{
Object myObject = new Object()//create a new instance of your object

myObject = copy //Make copy equal to your new object
//you should now have two exact copies (myObject
and copy)

return myObject;

}

hope this helps

Marco
 
Back
Top