因为手动复制字段是我创建代码生成器的最快方式,它读取您的类定义并生成克隆方法。您所需要的只是CGbR nuget 包和实现ICloneable
. 生成器将完成其余的工作。
public partial class Root : ICloneable
{
public Root(int number)
{
_number = number;
}
private int _number;
public Partial[] Partials { get; set; }
public IList<ulong> Numbers { get; set; }
public object Clone()
{
return Clone(true);
}
private Root()
{
}
}
public partial class Root
{
public Root Clone(bool deep)
{
var copy = new Root();
// All value types can be simply copied
copy._number = _number;
if (deep)
{
// In a deep clone the references are cloned
var tempPartials = new Partial[Partials.Length];
for (var i = 0; i < Partials.Length; i++)
{
var value = Partials[i];
value = value.Clone(true);
tempPartials[i] = value;
}
copy.Partials = tempPartials;
var tempNumbers = new List<ulong>(Numbers.Count);
for (var i = 0; i < Numbers.Count; i++)
{
var value = Numbers[i];
tempNumbers[i] = value;
}
copy.Numbers = tempNumbers;
}
else
{
// In a shallow clone only references are copied
copy.Partials = Partials;
copy.Numbers = Numbers;
}
return copy;
}
}
和部分类
public partial class Partial : ICloneable
{
public short Id { get; set; }
public string Name { get; set; }
public object Clone()
{
return Clone(true);
}
}
public partial class Partial
{
public Partial Clone(bool deep)
{
var copy = new Partial();
// All value types can be simply copied
copy.Id = Id;
copy.Name = Name;
return copy;
}
}