Bonk,
See inline:
[color=blue]
> a) use System.IClonable for deep copies and override
> object.MemberwiseClone for shallow copies ?[/color]
I would not recommend overriding MemberwiseClone. It's not going to get
you anything really. It's also very specific in what it does.
[color=blue]
> b) define my own IClonable, for example like this:
> interface IClonable <T>
> {
> /// <summary>
> /// creates a deep or shallow copy of the current object
> /// </summary>
> /// <param name="deep">
> /// if <c>true</c> a deep copy should be returned,
> /// wich means that the copied object and all
> /// objects referenced by the object are copied recursively
> /// if <c>false</c>a shallow copy should be retruned,
> /// wich means that only the top level references
> /// are copied
> ///</param>
> /// <returns>a deep or a shallow copy of the current
> object</returns>
> T Clone (bool deep);
> }[/color]
Making cloning interfaces generic isn't the best idea. Most frameworks
that would take advantage of this don't care what T is, they just need a
copy.
[color=blue]
> c) do something else I did not think of yet ?[/color]
I think that the advice in the article is still the best piece of advice
that there is for your specific need (to determine whether or not it can do
a deep clone or a shallow clone). Basically, define a IDeepClonable
interface, and then a IShallowClonable interface.
There is one problem though. If you have an object that has references
to other objects and you do a deep copy, what do you do when those other
objects don't support deep-copy semantics?
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
Join Bytes!