-
Notifications
You must be signed in to change notification settings - Fork 0
Clone Support
If an interface inherits the ICloneable interface then the object Clone() method will be implemented and a class constructor will be generated that can accept a non-null instance of the class for cloning. Here is a simple example of how it works:
[Generate]
public interface IHaveASimpleProperty : ICloneable
{
Guid Id { get; set; }
}
The generated code will look like:
public class HaveASimplePropertyModel : IHaveASimpleProperty
{
public System.Guid Id
{
get
{
return _Id;
}
set
{
_Id = value;
}
}
private System.Guid _Id;
public HaveASimplePropertyModel()
{
}
/// <summary>
/// Creates a copy of HaveASimplePropertyModel.
/// </summary>
public HaveASimplePropertyModel([System.Diagnostics.CodeAnalysis.NotNullAttribute]HaveASimplePropertyModel clone)
{
if (clone == null) throw new System.ArgumentNullException("clone");
this._Id = clone._Id;
}
/// <summary>
/// Creates a new object that is a copy of the current instance.
/// </summary>
public virtual object Clone() => new HaveASimplePropertyModel(this);
}
It is recommended for interfaces that use cloning that each member have a clone friendly type. Some types will not be cloned, but copied instead. If the type is a struct, primitive, or string then the value will be copied. If the type is a collection or array then each element in the collection will be cloned along with the collection or array instance using these same cloning rules. If the type implements the ICloneable then the value will be cloned using the Clone method. Finally, the value will be copied which means the value may not be cloned.