MemberwiseClone は、ディープ コピー ( MSDN )を実行するための適切な選択ではありません。
MemberwiseClone メソッドは、新しいオブジェクトを作成し、現在のオブジェクトの非静的フィールドを新しいオブジェクトにコピーすることによって浅いコピーを作成します。フィールドが値型の場合、フィールドのビットごとのコピーが実行されます。フィールドが参照型の場合、参照はコピーされますが、参照されるオブジェクトはコピーされません。したがって、元のオブジェクトとそのクローンは同じオブジェクトを参照します。
これは、複製されたオブジェクトに参照型の public フィールドまたはプロパティがある場合、元のオブジェクトのフィールド/プロパティと同じメモリ位置を参照するため、複製されたオブジェクトの各変更が初期オブジェクトに反映されることを意味します。これは真のディープ コピーではありません。
BinarySerialization を使用して、オブジェクトの完全に独立したインスタンスを作成できます。シリアル化の例については、 BinaryFormatter クラスの MSDN ページを参照してください。
例とテスト ハーネス:
特定のオブジェクトのディープ コピーを作成する拡張メソッド:
public static class MemoryUtils
{
/// <summary>
/// Creates a deep copy of a given object instance
/// </summary>
/// <typeparam name="TObject">Type of a given object</typeparam>
/// <param name="instance">Object to be cloned</param>
/// <param name="throwInCaseOfError">
/// A value which indicating whether exception should be thrown in case of
/// error whils clonin</param>
/// <returns>Returns a deep copy of a given object</returns>
/// <remarks>Uses BInarySerialization to create a true deep copy</remarks>
public static TObject DeepCopy<TObject>(this TObject instance, bool throwInCaseOfError)
where TObject : class
{
if (instance == null)
{
throw new ArgumentNullException("instance");
}
TObject clonedInstance = default(TObject);
try
{
using (var stream = new MemoryStream())
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(stream, instance);
// reset position to the beginning of the stream so
// deserialize would be able to deserialize an object instance
stream.Position = 0;
clonedInstance = (TObject)binaryFormatter.Deserialize(stream);
}
}
catch (Exception exception)
{
string errorMessage = String.Format(CultureInfo.CurrentCulture,
"Exception Type: {0}, Message: {1}{2}",
exception.GetType(),
exception.Message,
exception.InnerException == null ? String.Empty :
String.Format(CultureInfo.CurrentCulture,
" InnerException Type: {0}, Message: {1}",
exception.InnerException.GetType(),
exception.InnerException.Message));
Debug.WriteLine(errorMessage);
if (throwInCaseOfError)
{
throw;
}
}
return clonedInstance;
}
}
NUnit テスト:
public class MemoryUtilsFixture
{
[Test]
public void DeepCopyThrowWhenCopyInstanceOfNonSerializableType()
{
var nonSerializableInstance = new CustomNonSerializableType();
Assert.Throws<SerializationException>(() => nonSerializableInstance.DeepCopy(true));
}
[Test]
public void DeepCopyThrowWhenPassedInNull()
{
object instance = null;
Assert.Throws<ArgumentNullException>(() => instance.DeepCopy(true));
}
[Test]
public void DeepCopyThrowWhenCopyInstanceOfNonSerializableTypeAndErrorsDisabled()
{
var nonSerializableInstance = new CustomNonSerializableType();
object result = null;
Assert.DoesNotThrow(() => result = nonSerializableInstance.DeepCopy(false));
Assert.IsNull(result);
}
[Test]
public void DeepCopyShouldCreateExactAndIndependentCopyOfAnObject()
{
var instance = new CustomSerializableType
{
DateTimeValueType =
DateTime.Now.AddDays(1).AddMilliseconds(123).AddTicks(123),
NumericValueType = 777,
StringValueType = Guid.NewGuid().ToString(),
ReferenceType =
new CustomSerializableType
{
DateTimeValueType = DateTime.Now,
StringValueType = Guid.NewGuid().ToString()
}
};
var deepCopy = instance.DeepCopy(true);
Assert.IsNotNull(deepCopy);
Assert.IsFalse(ReferenceEquals(instance, deepCopy));
Assert.That(instance.NumericValueType == deepCopy.NumericValueType);
Assert.That(instance.DateTimeValueType == deepCopy.DateTimeValueType);
Assert.That(instance.StringValueType == deepCopy.StringValueType);
Assert.IsNotNull(deepCopy.ReferenceType);
Assert.IsFalse(ReferenceEquals(instance.ReferenceType, deepCopy.ReferenceType));
Assert.That(instance.ReferenceType.DateTimeValueType == deepCopy.ReferenceType.DateTimeValueType);
Assert.That(instance.ReferenceType.StringValueType == deepCopy.ReferenceType.StringValueType);
}
[Serializable]
internal sealed class CustomSerializableType
{
public int NumericValueType { get; set; }
public string StringValueType { get; set; }
public DateTime DateTimeValueType { get; set; }
public CustomSerializableType ReferenceType { get; set; }
}
public sealed class CustomNonSerializableType
{
}
}