I have the following class that I would like to do a deep copy on.
public class KeyInfo
{
public KeyInfo(IKeySetup keySetup, IKeyData data)
{
KeySetup = keySetup;
Data = data;
}
public IKeySetup KeySetup { get; set; }
public IKeyData Data { get; set; }
public KeyInfo DeepCopy()
{
var keyInfo = (KeyInfo) this.MemberwiseClone();
return keyInfo;
}
}
How can I copy the interfaces? Do I have to implement ICloneable for the interfaces and then have every class with one of those interfaces implement Clone()? Is there a way to avoid each class having to implement such a function?
Since there is no built in way of doing deep copy of an object you must provide your own to be able to do so.
Forcing implementer of
IKeySetup/IKeyDatato have DeepCopy by including it in interface may be good idea. RequiringICloneablecould be another approach – either compile time (by deriving yourIKeySetup/IKeyDatafromICloneable) or run-time by trhowing if object does not supportICloneable.