Duplicate instance.

  • Thread starter Thread starter Tiësto
  • Start date Start date
T

Tiësto

Hi everybody. I know everyone has said to me that this doesn't exist but I'm
going to try anyway.

I have an instance of ClassA and I want to duplicate that instance, creating
another one that is independient of the first one. Isn't there any automatic
way to do this?
Do I have to write IClonable implementation for each and every class?

Best Regards
 
System.Object has the method MemberwiseClone(). This will create a shallow
copy and not a deep copy. This means that reference types will be shared
across the copies. Value types in contrary will be copied.

If you are familiar with the book "Design Patterns" written by the gang of
four you should take a look at the Prototype pattern.

Gabriel Lozano-Morán
Software Engineer
Sogeti
 
Gracias Gabriel. I see this topic is a little complex, as I've seen many
different positions about where/how long/why we should use IClonable
interface or not. Some people say it will be deprecated on framework 2.0, as
you never know if it returns a shallow copy or a deep copy. I thought there
would be a method like MemberwiseClone(), with the diference that it could
create a real copy of the reference types, creating new memory locations and
copying the values from the first one.

I see i have to implement that by myself.

Thanks anyway!
 
One thing that you can do is to implement a copy constructor. Like:

public ClassA( ClassA classA )
{
// Create the copy object here
}

And later in your program's actual processing part:
....
ClassA originalClass = new ClassA();
....
ClassA copyClass = new ClassA( originalClass );
....

Daryush
 
I have taken a look at several classes in the .NET framework that implement
the IClonable interface. What these actually do is create a new object
instance using parameterized constructors. What you could try but I am not
sure that this will work is mark your class with the [Serializable]
attribute and then serialize it to a memory stream and then deserialize it.

Gabriel Lozano-Morán
Software Engineer
Sogeti
 
The main problem, I see, is to create "indepependent copies" of the
reference types.

I imagine a recursive method that, given an instance of a class, will create
a copy of it and all of its value type values. But when it finds a reference
type, it should run this method recursively to create a new copy, and so,
and so...

BUT. (and now I think I'm really understanding the purpose of ICloneable) we
couldn't do this beacause not all classes have a default argument-less
constructor. So, we couldn't know in advance HOW the instance is to be
created. And as the "new" keyword is mandatory in order to allocate new
memory for the new instance (and thus creating independent instances) .

In an ideal escenario, all the classes would have a Clone method, which
creates a shallow copy and calls Clone method of every reference type, or
manages to do the same with strings, which are also ref types.

I'm just making questions to myself. Feel free to post your comments.
Regards...


Gabriel Lozano-Morán said:
I have taken a look at several classes in the .NET framework that implement
the IClonable interface. What these actually do is create a new object
instance using parameterized constructors. What you could try but I am not
sure that this will work is mark your class with the [Serializable]
attribute and then serialize it to a memory stream and then deserialize it.

Gabriel Lozano-Morán
Software Engineer
Sogeti

Tiësto said:
Gracias Gabriel. I see this topic is a little complex, as I've seen many
different positions about where/how long/why we should use IClonable
interface or not. Some people say it will be deprecated on framework 2.0,
as you never know if it returns a shallow copy or a deep copy. I thought
there would be a method like MemberwiseClone(), with the diference that
it could create a real copy of the reference types, creating new memory
locations and copying the values from the first one.

I see i have to implement that by myself.

Thanks anyway!
 
Gabriel Lozano-Morán said:
I have taken a look at several classes in the .NET framework that implement
the IClonable interface. What these actually do is create a new object
instance using parameterized constructors. What you could try but I am not
sure that this will work is mark your class with the [Serializable]
attribute and then serialize it to a memory stream and then deserialize it.

<Shameful confession> I've done that before now. It works. </>
 
The difficulty is one of semantics and domain meaning. I, too,
struggled with this problem a while back and never came to a
satisfactory conclusion.

The only conclusion I did come to is that there is no "blanket
solution" that fits all scenarios. For some objects in my problem
domain, a shallow copy is exactly what I want. For others, a deep copy
is what I want. For still others, they need something in between.

For example, if an object holds a reference to an immutable reference
type (that is, a class that has been designed so that once constructed
it can't be changed), then there's no sense in copying it, since (apart
from threading considerations) two objects holding a reference to the
same object causes no surprises.

On the other hand, making a shallow copy of a tree structure is just
plain useless.

Even for a single class, sometimes a shallow copy makes sense, and in
other situations in my code I need a deep copy.

I gave up.

(By the way, Equals() suffers from the same malaise: when are two
objects really equal? What does that mean? Same bugbear in a different
disguise.)
 
Back
Top