Delphi has a standard mechanism where one object can be copied from another, paraphrased here as:
class Persistent
{
protected AssignError(Persistent source)
{
throw new ConvertError("Cannot assign a {0} to a {1}", source.GetType().Name, this.GetType().Name);
}
protected virtual AssignTo(Persistent destination)
{
destination.AssignError(this);
}
public virtual Assign(Persistent source)
{
source.AssignTo(this);
}
}
Does the .NET FCL has a canonical syntax of copying objects between each other?
e.g.:
interface IPersistent
{
public virtual Assign(Object source);
}
public class User : IPersistent
{
private Image avatarThumbnail;
private String name;
public override Assign(Object source)
{
if (source is User)
{
this.avatarThumbnail = source.avatarThumbnail;
this.name = source.Name;
}
else if (source is Image)
avatarThumbnail = (Image)source;
else if (source is DirectoryEntry)
name = ((DirectoryEntry) source).Firstname + " " + ((DirectoryEntry) source).Lastname;
else
throw new AssignError("Assign from {0}", source.GetType());
}
}
Yes i just invented a standard IPersistent
interface; but is there already a mechanism to copy objects between each other?
Update
Note: i am talking about the opposite of cloning an object.
User originalUser = new User();
User theClone = originalUser.Clone();
theClone.Lastname = "Guyer";
originalUser.Assign(theClone);
Or, i don't even need to have a clone at all:
User u = new User();
u.Assign(salesOrder); //copy name/e-mail from the sales order
or
SalesOrder s = new SalesOrder();
s.SalesOrderDate = DateTime.Now;
User customer = new Customer("Kirsten");
s.Assign(user); //copy sold-to/ship-to information from the user