Added a cloner that uses a binary serializer to clone objects; converted unit tests for reflection cloner into a shared base class so they can be applied to the serialization-based cloner as well
git-svn-id: file:///srv/devel/repo-conversion/nusu@227 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
7e61e27050
commit
961f56157e
|
@ -60,6 +60,7 @@
|
||||||
<Compile Include="Source\AffineThreadPool.Test.cs">
|
<Compile Include="Source\AffineThreadPool.Test.cs">
|
||||||
<DependentUpon>AffineThreadPool.cs</DependentUpon>
|
<DependentUpon>AffineThreadPool.cs</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="Source\Cloning\CloneFactoryTest.cs" />
|
||||||
<Compile Include="Source\Cloning\CloningParameters.cs" />
|
<Compile Include="Source\Cloning\CloningParameters.cs" />
|
||||||
<Compile Include="Source\Cloning\ExpressionTreeCloner.cs" />
|
<Compile Include="Source\Cloning\ExpressionTreeCloner.cs" />
|
||||||
<Compile Include="Source\Cloning\IStateCopier.cs" />
|
<Compile Include="Source\Cloning\IStateCopier.cs" />
|
||||||
|
@ -68,6 +69,10 @@
|
||||||
<DependentUpon>ReflectionCloner.cs</DependentUpon>
|
<DependentUpon>ReflectionCloner.cs</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="Source\Cloning\ICloneFactory.cs" />
|
<Compile Include="Source\Cloning\ICloneFactory.cs" />
|
||||||
|
<Compile Include="Source\Cloning\SerializationCloner.cs" />
|
||||||
|
<Compile Include="Source\Cloning\SerializationCloner.Test.cs">
|
||||||
|
<DependentUpon>SerializationCloner.cs</DependentUpon>
|
||||||
|
</Compile>
|
||||||
<Compile Include="Source\Collections\Deque.cs" />
|
<Compile Include="Source\Collections\Deque.cs" />
|
||||||
<Compile Include="Source\Collections\Deque.Insertion.cs">
|
<Compile Include="Source\Collections\Deque.Insertion.cs">
|
||||||
<DependentUpon>Deque.cs</DependentUpon>
|
<DependentUpon>Deque.cs</DependentUpon>
|
||||||
|
|
|
@ -91,6 +91,7 @@
|
||||||
<Compile Include="Source\AffineThreadPool.Test.cs">
|
<Compile Include="Source\AffineThreadPool.Test.cs">
|
||||||
<DependentUpon>AffineThreadPool.cs</DependentUpon>
|
<DependentUpon>AffineThreadPool.cs</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="Source\Cloning\CloneFactoryTest.cs" />
|
||||||
<Compile Include="Source\Cloning\CloningParameters.cs" />
|
<Compile Include="Source\Cloning\CloningParameters.cs" />
|
||||||
<Compile Include="Source\Cloning\ExpressionTreeCloner.cs" />
|
<Compile Include="Source\Cloning\ExpressionTreeCloner.cs" />
|
||||||
<Compile Include="Source\Cloning\ICloneFactory.cs" />
|
<Compile Include="Source\Cloning\ICloneFactory.cs" />
|
||||||
|
@ -99,6 +100,10 @@
|
||||||
<Compile Include="Source\Cloning\ReflectionCloner.Test.cs">
|
<Compile Include="Source\Cloning\ReflectionCloner.Test.cs">
|
||||||
<DependentUpon>ReflectionCloner.cs</DependentUpon>
|
<DependentUpon>ReflectionCloner.cs</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="Source\Cloning\SerializationCloner.cs" />
|
||||||
|
<Compile Include="Source\Cloning\SerializationCloner.Test.cs">
|
||||||
|
<DependentUpon>SerializationCloner.cs</DependentUpon>
|
||||||
|
</Compile>
|
||||||
<Compile Include="Source\Collections\Deque.cs" />
|
<Compile Include="Source\Collections\Deque.cs" />
|
||||||
<Compile Include="Source\Collections\Deque.Insertion.cs">
|
<Compile Include="Source\Collections\Deque.Insertion.cs">
|
||||||
<DependentUpon>Deque.cs</DependentUpon>
|
<DependentUpon>Deque.cs</DependentUpon>
|
||||||
|
|
|
@ -102,6 +102,7 @@
|
||||||
<Compile Include="Source\AffineThreadPool.Test.cs">
|
<Compile Include="Source\AffineThreadPool.Test.cs">
|
||||||
<DependentUpon>AffineThreadPool.cs</DependentUpon>
|
<DependentUpon>AffineThreadPool.cs</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="Source\Cloning\CloneFactoryTest.cs" />
|
||||||
<Compile Include="Source\Cloning\CloningParameters.cs" />
|
<Compile Include="Source\Cloning\CloningParameters.cs" />
|
||||||
<Compile Include="Source\Cloning\ExpressionTreeCloner.cs" />
|
<Compile Include="Source\Cloning\ExpressionTreeCloner.cs" />
|
||||||
<Compile Include="Source\Cloning\ICloneFactory.cs" />
|
<Compile Include="Source\Cloning\ICloneFactory.cs" />
|
||||||
|
@ -110,6 +111,10 @@
|
||||||
<Compile Include="Source\Cloning\ReflectionCloner.Test.cs">
|
<Compile Include="Source\Cloning\ReflectionCloner.Test.cs">
|
||||||
<DependentUpon>ReflectionCloner.cs</DependentUpon>
|
<DependentUpon>ReflectionCloner.cs</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="Source\Cloning\SerializationCloner.cs" />
|
||||||
|
<Compile Include="Source\Cloning\SerializationCloner.Test.cs">
|
||||||
|
<DependentUpon>SerializationCloner.cs</DependentUpon>
|
||||||
|
</Compile>
|
||||||
<Compile Include="Source\Collections\Deque.cs" />
|
<Compile Include="Source\Collections\Deque.cs" />
|
||||||
<Compile Include="Source\Collections\Deque.Insertion.cs">
|
<Compile Include="Source\Collections\Deque.Insertion.cs">
|
||||||
<DependentUpon>Deque.cs</DependentUpon>
|
<DependentUpon>Deque.cs</DependentUpon>
|
||||||
|
|
446
Source/Cloning/CloneFactoryTest.cs
Normal file
446
Source/Cloning/CloneFactoryTest.cs
Normal file
|
@ -0,0 +1,446 @@
|
||||||
|
#region CPL License
|
||||||
|
/*
|
||||||
|
Nuclex Framework
|
||||||
|
Copyright (C) 2002-2010 Nuclex Development Labs
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the IBM Common Public License as
|
||||||
|
published by the IBM Corporation; either version 1.0 of the
|
||||||
|
License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
IBM Common Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the IBM Common Public
|
||||||
|
License along with this library
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#if UNITTEST
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace Nuclex.Support.Cloning {
|
||||||
|
|
||||||
|
/// <summary>Base class for unit tests verifying the clone factory</summary>
|
||||||
|
public abstract class CloneFactoryTest {
|
||||||
|
|
||||||
|
#region class TestReferenceType
|
||||||
|
|
||||||
|
/// <summary>A reference type being used for testing</summary>
|
||||||
|
protected class TestReferenceType {
|
||||||
|
|
||||||
|
/// <summary>Field holding an integer value for testing</summary>
|
||||||
|
public int TestField;
|
||||||
|
/// <summary>Property holding an integer value for testing</summary>
|
||||||
|
public int TestProperty { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // class TestReferenceType
|
||||||
|
|
||||||
|
#region struct TestValueType
|
||||||
|
|
||||||
|
/// <summary>A value type being used for testing</summary>
|
||||||
|
protected struct TestValueType {
|
||||||
|
|
||||||
|
/// <summary>Field holding an integer value for testing</summary>
|
||||||
|
public int TestField;
|
||||||
|
/// <summary>Property holding an integer value for testing</summary>
|
||||||
|
public int TestProperty { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // struct TestValueType
|
||||||
|
|
||||||
|
#region struct HierarchicalValueType
|
||||||
|
|
||||||
|
/// <summary>A value type containiner other complex types used for testing</summary>
|
||||||
|
protected struct HierarchicalValueType {
|
||||||
|
|
||||||
|
/// <summary>Field holding an integer value for testing</summary>
|
||||||
|
public int TestField;
|
||||||
|
/// <summary>Property holding an integer value for testing</summary>
|
||||||
|
public int TestProperty { get; set; }
|
||||||
|
/// <summary>Value type field for testing</summary>
|
||||||
|
public TestValueType ValueTypeField;
|
||||||
|
/// <summary>Value type property for testing</summary>
|
||||||
|
public TestValueType ValueTypeProperty { get; set; }
|
||||||
|
/// <summary>Reference type field for testing</summary>
|
||||||
|
public TestReferenceType ReferenceTypeField;
|
||||||
|
/// <summary>Reference type property for testing</summary>
|
||||||
|
public TestReferenceType ReferenceTypeProperty { get; set; }
|
||||||
|
/// <summary>An array field of reference types</summary>
|
||||||
|
public TestReferenceType[,][] ReferenceTypeArrayField;
|
||||||
|
/// <summary>An array property of reference types</summary>
|
||||||
|
public TestReferenceType[,][] ReferenceTypeArrayProperty { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // struct HierarchicalValueType
|
||||||
|
|
||||||
|
#region struct HierarchicalReferenceType
|
||||||
|
|
||||||
|
/// <summary>A value type containiner other complex types used for testing</summary>
|
||||||
|
protected class HierarchicalReferenceType {
|
||||||
|
|
||||||
|
/// <summary>Field holding an integer value for testing</summary>
|
||||||
|
public int TestField;
|
||||||
|
/// <summary>Property holding an integer value for testing</summary>
|
||||||
|
public int TestProperty { get; set; }
|
||||||
|
/// <summary>Value type field for testing</summary>
|
||||||
|
public TestValueType ValueTypeField;
|
||||||
|
/// <summary>Value type property for testing</summary>
|
||||||
|
public TestValueType ValueTypeProperty { get; set; }
|
||||||
|
/// <summary>Reference type field for testing</summary>
|
||||||
|
public TestReferenceType ReferenceTypeField;
|
||||||
|
/// <summary>Reference type property for testing</summary>
|
||||||
|
public TestReferenceType ReferenceTypeProperty { get; set; }
|
||||||
|
/// <summary>An array field of reference types</summary>
|
||||||
|
public TestReferenceType[,][] ReferenceTypeArrayField;
|
||||||
|
/// <summary>An array property of reference types</summary>
|
||||||
|
public TestReferenceType[,][] ReferenceTypeArrayProperty { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // struct HierarchicalReferenceType
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that a cloned object exhibits the expected state for the type of
|
||||||
|
/// clone that has been performed
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="original">Original instance the clone was created from</param>
|
||||||
|
/// <param name="clone">Cloned instance that will be checked for correctness</param>
|
||||||
|
/// <param name="isDeepClone">Whether the cloned instance is a deep clone</param>
|
||||||
|
/// <param name="isPropertyBasedClone">
|
||||||
|
/// Whether a property-based clone was performed
|
||||||
|
/// </param>
|
||||||
|
protected static void VerifyClone(
|
||||||
|
HierarchicalReferenceType original, HierarchicalReferenceType clone,
|
||||||
|
bool isDeepClone, bool isPropertyBasedClone
|
||||||
|
) {
|
||||||
|
if(isPropertyBasedClone) {
|
||||||
|
Assert.AreEqual(0, clone.TestField);
|
||||||
|
Assert.AreEqual(0, clone.ValueTypeField.TestField);
|
||||||
|
Assert.AreEqual(0, clone.ValueTypeField.TestProperty);
|
||||||
|
Assert.AreEqual(0, clone.ValueTypeProperty.TestField);
|
||||||
|
Assert.IsNull(clone.ReferenceTypeField);
|
||||||
|
|
||||||
|
if(isDeepClone) {
|
||||||
|
Assert.AreNotSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
|
||||||
|
Assert.AreNotSame(
|
||||||
|
original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
|
||||||
|
);
|
||||||
|
Assert.AreEqual(0, clone.ReferenceTypeProperty.TestField);
|
||||||
|
Assert.AreNotSame(
|
||||||
|
original.ReferenceTypeArrayProperty[1, 3][0],
|
||||||
|
clone.ReferenceTypeArrayProperty[1, 3][0]
|
||||||
|
);
|
||||||
|
Assert.AreNotSame(
|
||||||
|
original.ReferenceTypeArrayProperty[1, 3][2],
|
||||||
|
clone.ReferenceTypeArrayProperty[1, 3][2]
|
||||||
|
);
|
||||||
|
Assert.AreEqual(0, clone.ReferenceTypeArrayProperty[1, 3][0].TestField);
|
||||||
|
Assert.AreEqual(0, clone.ReferenceTypeArrayProperty[1, 3][2].TestField);
|
||||||
|
} else {
|
||||||
|
Assert.AreSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
|
||||||
|
Assert.AreSame(
|
||||||
|
original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Assert.AreEqual(original.TestField, clone.TestField);
|
||||||
|
Assert.AreEqual(original.ValueTypeField.TestField, clone.ValueTypeField.TestField);
|
||||||
|
Assert.AreEqual(original.ValueTypeField.TestProperty, clone.ValueTypeField.TestProperty);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ValueTypeProperty.TestField, clone.ValueTypeProperty.TestField
|
||||||
|
);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ReferenceTypeField.TestField, clone.ReferenceTypeField.TestField
|
||||||
|
);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ReferenceTypeField.TestProperty, clone.ReferenceTypeField.TestProperty
|
||||||
|
);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ReferenceTypeProperty.TestField, clone.ReferenceTypeProperty.TestField
|
||||||
|
);
|
||||||
|
|
||||||
|
if(isDeepClone) {
|
||||||
|
Assert.AreNotSame(original.ReferenceTypeField, clone.ReferenceTypeField);
|
||||||
|
Assert.AreNotSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
|
||||||
|
Assert.AreNotSame(
|
||||||
|
original.ReferenceTypeArrayField, clone.ReferenceTypeArrayField
|
||||||
|
);
|
||||||
|
Assert.AreNotSame(
|
||||||
|
original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
|
||||||
|
);
|
||||||
|
Assert.AreNotSame(
|
||||||
|
original.ReferenceTypeArrayProperty[1, 3][0],
|
||||||
|
clone.ReferenceTypeArrayProperty[1, 3][0]
|
||||||
|
);
|
||||||
|
Assert.AreNotSame(
|
||||||
|
original.ReferenceTypeArrayProperty[1, 3][2],
|
||||||
|
clone.ReferenceTypeArrayProperty[1, 3][2]
|
||||||
|
);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ReferenceTypeArrayProperty[1, 3][0].TestField,
|
||||||
|
clone.ReferenceTypeArrayProperty[1, 3][0].TestField
|
||||||
|
);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ReferenceTypeArrayProperty[1, 3][2].TestField,
|
||||||
|
clone.ReferenceTypeArrayProperty[1, 3][2].TestField
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
Assert.AreSame(original.ReferenceTypeField, clone.ReferenceTypeField);
|
||||||
|
Assert.AreSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
|
||||||
|
Assert.AreSame(
|
||||||
|
original.ReferenceTypeArrayField, clone.ReferenceTypeArrayField
|
||||||
|
);
|
||||||
|
Assert.AreSame(
|
||||||
|
original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.AreEqual(original.TestProperty, clone.TestProperty);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ValueTypeProperty.TestProperty, clone.ValueTypeProperty.TestProperty
|
||||||
|
);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ReferenceTypeProperty.TestProperty, clone.ReferenceTypeProperty.TestProperty
|
||||||
|
);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ReferenceTypeArrayProperty[1, 3][0].TestProperty,
|
||||||
|
clone.ReferenceTypeArrayProperty[1, 3][0].TestProperty
|
||||||
|
);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ReferenceTypeArrayProperty[1, 3][2].TestProperty,
|
||||||
|
clone.ReferenceTypeArrayProperty[1, 3][2].TestProperty
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that a cloned object exhibits the expected state for the type of
|
||||||
|
/// clone that has been performed
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="original">Original instance the clone was created from</param>
|
||||||
|
/// <param name="clone">Cloned instance that will be checked for correctness</param>
|
||||||
|
/// <param name="isDeepClone">Whether the cloned instance is a deep clone</param>
|
||||||
|
/// <param name="isPropertyBasedClone">
|
||||||
|
/// Whether a property-based clone was performed
|
||||||
|
/// </param>
|
||||||
|
protected static void VerifyClone(
|
||||||
|
ref HierarchicalValueType original, ref HierarchicalValueType clone,
|
||||||
|
bool isDeepClone, bool isPropertyBasedClone
|
||||||
|
) {
|
||||||
|
if(isPropertyBasedClone) {
|
||||||
|
Assert.AreEqual(0, clone.TestField);
|
||||||
|
Assert.AreEqual(0, clone.ValueTypeField.TestField);
|
||||||
|
Assert.AreEqual(0, clone.ValueTypeField.TestProperty);
|
||||||
|
Assert.AreEqual(0, clone.ValueTypeProperty.TestField);
|
||||||
|
Assert.IsNull(clone.ReferenceTypeField);
|
||||||
|
|
||||||
|
if(isDeepClone) {
|
||||||
|
Assert.AreNotSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
|
||||||
|
Assert.AreNotSame(
|
||||||
|
original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
|
||||||
|
);
|
||||||
|
Assert.AreEqual(0, clone.ReferenceTypeProperty.TestField);
|
||||||
|
Assert.AreNotSame(
|
||||||
|
original.ReferenceTypeArrayProperty[1, 3][0],
|
||||||
|
clone.ReferenceTypeArrayProperty[1, 3][0]
|
||||||
|
);
|
||||||
|
Assert.AreNotSame(
|
||||||
|
original.ReferenceTypeArrayProperty[1, 3][2],
|
||||||
|
clone.ReferenceTypeArrayProperty[1, 3][2]
|
||||||
|
);
|
||||||
|
Assert.AreEqual(0, clone.ReferenceTypeArrayProperty[1, 3][0].TestField);
|
||||||
|
Assert.AreEqual(0, clone.ReferenceTypeArrayProperty[1, 3][2].TestField);
|
||||||
|
} else {
|
||||||
|
Assert.AreSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
|
||||||
|
Assert.AreSame(
|
||||||
|
original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Assert.AreEqual(original.TestField, clone.TestField);
|
||||||
|
Assert.AreEqual(original.ValueTypeField.TestField, clone.ValueTypeField.TestField);
|
||||||
|
Assert.AreEqual(original.ValueTypeField.TestProperty, clone.ValueTypeField.TestProperty);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ValueTypeProperty.TestField, clone.ValueTypeProperty.TestField
|
||||||
|
);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ReferenceTypeField.TestField, clone.ReferenceTypeField.TestField
|
||||||
|
);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ReferenceTypeField.TestProperty, clone.ReferenceTypeField.TestProperty
|
||||||
|
);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ReferenceTypeProperty.TestField, clone.ReferenceTypeProperty.TestField
|
||||||
|
);
|
||||||
|
|
||||||
|
if(isDeepClone) {
|
||||||
|
Assert.AreNotSame(original.ReferenceTypeField, clone.ReferenceTypeField);
|
||||||
|
Assert.AreNotSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
|
||||||
|
Assert.AreNotSame(
|
||||||
|
original.ReferenceTypeArrayField, clone.ReferenceTypeArrayField
|
||||||
|
);
|
||||||
|
Assert.AreNotSame(
|
||||||
|
original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
|
||||||
|
);
|
||||||
|
Assert.AreNotSame(
|
||||||
|
original.ReferenceTypeArrayProperty[1, 3][0],
|
||||||
|
clone.ReferenceTypeArrayProperty[1, 3][0]
|
||||||
|
);
|
||||||
|
Assert.AreNotSame(
|
||||||
|
original.ReferenceTypeArrayProperty[1, 3][2],
|
||||||
|
clone.ReferenceTypeArrayProperty[1, 3][2]
|
||||||
|
);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ReferenceTypeArrayProperty[1, 3][0].TestField,
|
||||||
|
clone.ReferenceTypeArrayProperty[1, 3][0].TestField
|
||||||
|
);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ReferenceTypeArrayProperty[1, 3][2].TestField,
|
||||||
|
clone.ReferenceTypeArrayProperty[1, 3][2].TestField
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
Assert.AreSame(original.ReferenceTypeField, clone.ReferenceTypeField);
|
||||||
|
Assert.AreSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
|
||||||
|
Assert.AreSame(
|
||||||
|
original.ReferenceTypeArrayField, clone.ReferenceTypeArrayField
|
||||||
|
);
|
||||||
|
Assert.AreSame(
|
||||||
|
original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.AreEqual(original.TestProperty, clone.TestProperty);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ValueTypeProperty.TestProperty, clone.ValueTypeProperty.TestProperty
|
||||||
|
);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ReferenceTypeProperty.TestProperty, clone.ReferenceTypeProperty.TestProperty
|
||||||
|
);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ReferenceTypeArrayProperty[1, 3][0].TestProperty,
|
||||||
|
clone.ReferenceTypeArrayProperty[1, 3][0].TestProperty
|
||||||
|
);
|
||||||
|
Assert.AreEqual(
|
||||||
|
original.ReferenceTypeArrayProperty[1, 3][2].TestProperty,
|
||||||
|
clone.ReferenceTypeArrayProperty[1, 3][2].TestProperty
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Creates a value type with random data for testing</summary>
|
||||||
|
/// <returns>A new value type with random data</returns>
|
||||||
|
protected static HierarchicalValueType CreateValueType() {
|
||||||
|
return new HierarchicalValueType() {
|
||||||
|
TestField = 123,
|
||||||
|
TestProperty = 321,
|
||||||
|
ReferenceTypeArrayField = new TestReferenceType[2, 4][] {
|
||||||
|
{
|
||||||
|
null, null, null, null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
null, null, null,
|
||||||
|
new TestReferenceType[3] {
|
||||||
|
new TestReferenceType() { TestField = 101, TestProperty = 202 },
|
||||||
|
null,
|
||||||
|
new TestReferenceType() { TestField = 909, TestProperty = 808 }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ReferenceTypeArrayProperty = new TestReferenceType[2, 4][] {
|
||||||
|
{
|
||||||
|
null, null, null, null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
null, null, null,
|
||||||
|
new TestReferenceType[3] {
|
||||||
|
new TestReferenceType() { TestField = 303, TestProperty = 404 },
|
||||||
|
null,
|
||||||
|
new TestReferenceType() { TestField = 707, TestProperty = 606 }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ValueTypeField = new TestValueType() {
|
||||||
|
TestField = 456,
|
||||||
|
TestProperty = 654
|
||||||
|
},
|
||||||
|
ValueTypeProperty = new TestValueType() {
|
||||||
|
TestField = 789,
|
||||||
|
TestProperty = 987,
|
||||||
|
},
|
||||||
|
ReferenceTypeField = new TestReferenceType() {
|
||||||
|
TestField = 135,
|
||||||
|
TestProperty = 531
|
||||||
|
},
|
||||||
|
ReferenceTypeProperty = new TestReferenceType() {
|
||||||
|
TestField = 246,
|
||||||
|
TestProperty = 642,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Creates a reference type with random data for testing</summary>
|
||||||
|
/// <returns>A new reference type with random data</returns>
|
||||||
|
protected static HierarchicalReferenceType CreateReferenceType() {
|
||||||
|
return new HierarchicalReferenceType() {
|
||||||
|
TestField = 123,
|
||||||
|
TestProperty = 321,
|
||||||
|
ReferenceTypeArrayField = new TestReferenceType[2, 4][] {
|
||||||
|
{
|
||||||
|
null, null, null, null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
null, null, null,
|
||||||
|
new TestReferenceType[3] {
|
||||||
|
new TestReferenceType() { TestField = 101, TestProperty = 202 },
|
||||||
|
null,
|
||||||
|
new TestReferenceType() { TestField = 909, TestProperty = 808 }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ReferenceTypeArrayProperty = new TestReferenceType[2, 4][] {
|
||||||
|
{
|
||||||
|
null, null, null, null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
null, null, null,
|
||||||
|
new TestReferenceType[3] {
|
||||||
|
new TestReferenceType() { TestField = 303, TestProperty = 404 },
|
||||||
|
null,
|
||||||
|
new TestReferenceType() { TestField = 707, TestProperty = 606 }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ValueTypeField = new TestValueType() {
|
||||||
|
TestField = 456,
|
||||||
|
TestProperty = 654
|
||||||
|
},
|
||||||
|
ValueTypeProperty = new TestValueType() {
|
||||||
|
TestField = 789,
|
||||||
|
TestProperty = 987,
|
||||||
|
},
|
||||||
|
ReferenceTypeField = new TestReferenceType() {
|
||||||
|
TestField = 135,
|
||||||
|
TestProperty = 531
|
||||||
|
},
|
||||||
|
ReferenceTypeProperty = new TestReferenceType() {
|
||||||
|
TestField = 246,
|
||||||
|
TestProperty = 642,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Nuclex.Support.Cloning
|
||||||
|
|
||||||
|
#endif // UNITTEST
|
|
@ -29,93 +29,18 @@ namespace Nuclex.Support.Cloning {
|
||||||
|
|
||||||
/// <summary>Unit Test for the reflection-based cloner</summary>
|
/// <summary>Unit Test for the reflection-based cloner</summary>
|
||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class ReflectionClonerTest {
|
public class ReflectionClonerTest : CloneFactoryTest {
|
||||||
|
|
||||||
#region class TestReferenceType
|
|
||||||
|
|
||||||
/// <summary>A reference type being used for testing</summary>
|
|
||||||
private class TestReferenceType {
|
|
||||||
|
|
||||||
/// <summary>Field holding an integer value for testing</summary>
|
|
||||||
public int TestField;
|
|
||||||
/// <summary>Property holding an integer value for testing</summary>
|
|
||||||
public int TestProperty { get; set; }
|
|
||||||
|
|
||||||
|
/// <summary>Initializes a new unit test suite for the reflection cloner</summary>
|
||||||
|
public ReflectionClonerTest() {
|
||||||
|
this.cloneFactory = new ReflectionCloner();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion // class TestReferenceType
|
|
||||||
|
|
||||||
#region struct TestValueType
|
|
||||||
|
|
||||||
/// <summary>A value type being used for testing</summary>
|
|
||||||
private struct TestValueType {
|
|
||||||
|
|
||||||
/// <summary>Field holding an integer value for testing</summary>
|
|
||||||
public int TestField;
|
|
||||||
/// <summary>Property holding an integer value for testing</summary>
|
|
||||||
public int TestProperty { get; set; }
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion // struct TestValueType
|
|
||||||
|
|
||||||
#region struct HierarchicalValueType
|
|
||||||
|
|
||||||
/// <summary>A value type containiner other complex types used for testing</summary>
|
|
||||||
private struct HierarchicalValueType {
|
|
||||||
|
|
||||||
/// <summary>Field holding an integer value for testing</summary>
|
|
||||||
public int TestField;
|
|
||||||
/// <summary>Property holding an integer value for testing</summary>
|
|
||||||
public int TestProperty { get; set; }
|
|
||||||
/// <summary>Value type field for testing</summary>
|
|
||||||
public TestValueType ValueTypeField;
|
|
||||||
/// <summary>Value type property for testing</summary>
|
|
||||||
public TestValueType ValueTypeProperty { get; set; }
|
|
||||||
/// <summary>Reference type field for testing</summary>
|
|
||||||
public TestReferenceType ReferenceTypeField;
|
|
||||||
/// <summary>Reference type property for testing</summary>
|
|
||||||
public TestReferenceType ReferenceTypeProperty { get; set; }
|
|
||||||
/// <summary>An array field of reference types</summary>
|
|
||||||
public TestReferenceType[,][] ReferenceTypeArrayField;
|
|
||||||
/// <summary>An array property of reference types</summary>
|
|
||||||
public TestReferenceType[,][] ReferenceTypeArrayProperty { get; set; }
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion // struct HierarchicalValueType
|
|
||||||
|
|
||||||
#region struct HierarchicalReferenceType
|
|
||||||
|
|
||||||
/// <summary>A value type containiner other complex types used for testing</summary>
|
|
||||||
private class HierarchicalReferenceType {
|
|
||||||
|
|
||||||
/// <summary>Field holding an integer value for testing</summary>
|
|
||||||
public int TestField;
|
|
||||||
/// <summary>Property holding an integer value for testing</summary>
|
|
||||||
public int TestProperty { get; set; }
|
|
||||||
/// <summary>Value type field for testing</summary>
|
|
||||||
public TestValueType ValueTypeField;
|
|
||||||
/// <summary>Value type property for testing</summary>
|
|
||||||
public TestValueType ValueTypeProperty { get; set; }
|
|
||||||
/// <summary>Reference type field for testing</summary>
|
|
||||||
public TestReferenceType ReferenceTypeField;
|
|
||||||
/// <summary>Reference type property for testing</summary>
|
|
||||||
public TestReferenceType ReferenceTypeProperty { get; set; }
|
|
||||||
/// <summary>An array field of reference types</summary>
|
|
||||||
public TestReferenceType[,][] ReferenceTypeArrayField;
|
|
||||||
/// <summary>An array property of reference types</summary>
|
|
||||||
public TestReferenceType[,][] ReferenceTypeArrayProperty { get; set; }
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion // struct HierarchicalReferenceType
|
|
||||||
|
|
||||||
/// <summary>Verifies that clones of primitive types can be created</summary>
|
/// <summary>Verifies that clones of primitive types can be created</summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void PrimitiveTypesCanBeCloned() {
|
public void PrimitiveTypesCanBeCloned() {
|
||||||
int original = 12345;
|
int original = 12345;
|
||||||
int clone = (new ReflectionCloner()).ShallowClone(original, false);
|
int clone = this.cloneFactory.ShallowClone(original, false);
|
||||||
Assert.AreEqual(original, clone);
|
Assert.AreEqual(original, clone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,7 +50,7 @@ namespace Nuclex.Support.Cloning {
|
||||||
var original = new TestReferenceType[] {
|
var original = new TestReferenceType[] {
|
||||||
new TestReferenceType() { TestField = 123, TestProperty = 456 }
|
new TestReferenceType() { TestField = 123, TestProperty = 456 }
|
||||||
};
|
};
|
||||||
TestReferenceType[] clone = (new ReflectionCloner()).ShallowClone(original, false);
|
TestReferenceType[] clone = this.cloneFactory.ShallowClone(original, false);
|
||||||
|
|
||||||
Assert.AreSame(original[0], clone[0]);
|
Assert.AreSame(original[0], clone[0]);
|
||||||
}
|
}
|
||||||
|
@ -136,7 +61,7 @@ namespace Nuclex.Support.Cloning {
|
||||||
var original = new TestReferenceType[] {
|
var original = new TestReferenceType[] {
|
||||||
new TestReferenceType() { TestField = 123, TestProperty = 456 }
|
new TestReferenceType() { TestField = 123, TestProperty = 456 }
|
||||||
};
|
};
|
||||||
TestReferenceType[] clone = (new ReflectionCloner()).DeepClone(original, false);
|
TestReferenceType[] clone = this.cloneFactory.DeepClone(original, false);
|
||||||
|
|
||||||
Assert.AreNotSame(original[0], clone[0]);
|
Assert.AreNotSame(original[0], clone[0]);
|
||||||
Assert.AreEqual(original[0].TestField, clone[0].TestField);
|
Assert.AreEqual(original[0].TestField, clone[0].TestField);
|
||||||
|
@ -147,7 +72,7 @@ namespace Nuclex.Support.Cloning {
|
||||||
[Test]
|
[Test]
|
||||||
public void GenericListsCanBeCloned() {
|
public void GenericListsCanBeCloned() {
|
||||||
var original = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
|
var original = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
|
||||||
List<int> clone = (new ReflectionCloner()).DeepClone(original, false);
|
List<int> clone = this.cloneFactory.DeepClone(original, false);
|
||||||
|
|
||||||
CollectionAssert.AreEqual(original, clone);
|
CollectionAssert.AreEqual(original, clone);
|
||||||
}
|
}
|
||||||
|
@ -157,7 +82,7 @@ namespace Nuclex.Support.Cloning {
|
||||||
public void GenericDictionariesCanBeCloned() {
|
public void GenericDictionariesCanBeCloned() {
|
||||||
var original = new Dictionary<int, string>();
|
var original = new Dictionary<int, string>();
|
||||||
original.Add(1, "one");
|
original.Add(1, "one");
|
||||||
Dictionary<int, string> clone = (new ReflectionCloner()).DeepClone(original, false);
|
Dictionary<int, string> clone = this.cloneFactory.DeepClone(original, false);
|
||||||
|
|
||||||
Assert.AreEqual("one", clone[1]);
|
Assert.AreEqual("one", clone[1]);
|
||||||
}
|
}
|
||||||
|
@ -167,9 +92,9 @@ namespace Nuclex.Support.Cloning {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void ShallowFieldBasedClonesOfValueTypesCanBeMade() {
|
public void ShallowFieldBasedClonesOfValueTypesCanBeMade() {
|
||||||
HierarchicalValueType original = createValueType();
|
HierarchicalValueType original = CreateValueType();
|
||||||
HierarchicalValueType clone = (new ReflectionCloner()).ShallowClone(original, false);
|
HierarchicalValueType clone = this.cloneFactory.ShallowClone(original, false);
|
||||||
verifyClone(ref original, ref clone, isDeepClone: false, isPropertyBasedClone: false);
|
VerifyClone(ref original, ref clone, isDeepClone: false, isPropertyBasedClone: false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -177,9 +102,9 @@ namespace Nuclex.Support.Cloning {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void ShallowFieldBasedClonesOfReferenceTypesCanBeMade() {
|
public void ShallowFieldBasedClonesOfReferenceTypesCanBeMade() {
|
||||||
HierarchicalReferenceType original = createReferenceType();
|
HierarchicalReferenceType original = CreateReferenceType();
|
||||||
HierarchicalReferenceType clone = (new ReflectionCloner()).ShallowClone(original, false);
|
HierarchicalReferenceType clone = this.cloneFactory.ShallowClone(original, false);
|
||||||
verifyClone(original, clone, isDeepClone: false, isPropertyBasedClone: false);
|
VerifyClone(original, clone, isDeepClone: false, isPropertyBasedClone: false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -187,9 +112,9 @@ namespace Nuclex.Support.Cloning {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void DeepFieldBasedClonesOfValueTypesCanBeMade() {
|
public void DeepFieldBasedClonesOfValueTypesCanBeMade() {
|
||||||
HierarchicalValueType original = createValueType();
|
HierarchicalValueType original = CreateValueType();
|
||||||
HierarchicalValueType clone = (new ReflectionCloner()).DeepClone(original, false);
|
HierarchicalValueType clone = this.cloneFactory.DeepClone(original, false);
|
||||||
verifyClone(ref original, ref clone, isDeepClone: true, isPropertyBasedClone: false);
|
VerifyClone(ref original, ref clone, isDeepClone: true, isPropertyBasedClone: false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -197,9 +122,9 @@ namespace Nuclex.Support.Cloning {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void DeepFieldBasedClonesOfReferenceTypesCanBeMade() {
|
public void DeepFieldBasedClonesOfReferenceTypesCanBeMade() {
|
||||||
HierarchicalReferenceType original = createReferenceType();
|
HierarchicalReferenceType original = CreateReferenceType();
|
||||||
HierarchicalReferenceType clone = (new ReflectionCloner()).DeepClone(original, false);
|
HierarchicalReferenceType clone = this.cloneFactory.DeepClone(original, false);
|
||||||
verifyClone(original, clone, isDeepClone: true, isPropertyBasedClone: false);
|
VerifyClone(original, clone, isDeepClone: true, isPropertyBasedClone: false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -207,9 +132,9 @@ namespace Nuclex.Support.Cloning {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void ShallowPropertyBasedClonesOfValueTypesCanBeMade() {
|
public void ShallowPropertyBasedClonesOfValueTypesCanBeMade() {
|
||||||
HierarchicalValueType original = createValueType();
|
HierarchicalValueType original = CreateValueType();
|
||||||
HierarchicalValueType clone = (new ReflectionCloner()).ShallowClone(original, true);
|
HierarchicalValueType clone = this.cloneFactory.ShallowClone(original, true);
|
||||||
verifyClone(ref original, ref clone, isDeepClone: false, isPropertyBasedClone: true);
|
VerifyClone(ref original, ref clone, isDeepClone: false, isPropertyBasedClone: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -217,9 +142,9 @@ namespace Nuclex.Support.Cloning {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void ShallowPropertyBasedClonesOfReferenceTypesCanBeMade() {
|
public void ShallowPropertyBasedClonesOfReferenceTypesCanBeMade() {
|
||||||
HierarchicalReferenceType original = createReferenceType();
|
HierarchicalReferenceType original = CreateReferenceType();
|
||||||
HierarchicalReferenceType clone = (new ReflectionCloner()).ShallowClone(original, true);
|
HierarchicalReferenceType clone = this.cloneFactory.ShallowClone(original, true);
|
||||||
verifyClone(original, clone, isDeepClone: false, isPropertyBasedClone: true);
|
VerifyClone(original, clone, isDeepClone: false, isPropertyBasedClone: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -227,9 +152,9 @@ namespace Nuclex.Support.Cloning {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void DeepPropertyBasedClonesOfValueTypesCanBeMade() {
|
public void DeepPropertyBasedClonesOfValueTypesCanBeMade() {
|
||||||
HierarchicalValueType original = createValueType();
|
HierarchicalValueType original = CreateValueType();
|
||||||
HierarchicalValueType clone = (new ReflectionCloner()).DeepClone(original, true);
|
HierarchicalValueType clone = this.cloneFactory.DeepClone(original, true);
|
||||||
verifyClone(ref original, ref clone, isDeepClone: true, isPropertyBasedClone: true);
|
VerifyClone(ref original, ref clone, isDeepClone: true, isPropertyBasedClone: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -237,340 +162,13 @@ namespace Nuclex.Support.Cloning {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void DeepPropertyBasedClonesOfReferenceTypesCanBeMade() {
|
public void DeepPropertyBasedClonesOfReferenceTypesCanBeMade() {
|
||||||
HierarchicalReferenceType original = createReferenceType();
|
HierarchicalReferenceType original = CreateReferenceType();
|
||||||
HierarchicalReferenceType clone = (new ReflectionCloner()).DeepClone(original, true);
|
HierarchicalReferenceType clone = this.cloneFactory.DeepClone(original, true);
|
||||||
verifyClone(original, clone, isDeepClone: true, isPropertyBasedClone: true);
|
VerifyClone(original, clone, isDeepClone: true, isPropertyBasedClone: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>Clone factory being tested</summary>
|
||||||
/// Verifies that a cloned object exhibits the expected state for the type of
|
private ICloneFactory cloneFactory;
|
||||||
/// clone that has been performed
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="original">Original instance the clone was created from</param>
|
|
||||||
/// <param name="clone">Cloned instance that will be checked for correctness</param>
|
|
||||||
/// <param name="isDeepClone">Whether the cloned instance is a deep clone</param>
|
|
||||||
/// <param name="isPropertyBasedClone">
|
|
||||||
/// Whether a property-based clone was performed
|
|
||||||
/// </param>
|
|
||||||
private static void verifyClone(
|
|
||||||
HierarchicalReferenceType original, HierarchicalReferenceType clone,
|
|
||||||
bool isDeepClone, bool isPropertyBasedClone
|
|
||||||
) {
|
|
||||||
if(isPropertyBasedClone) {
|
|
||||||
Assert.AreEqual(0, clone.TestField);
|
|
||||||
Assert.AreEqual(0, clone.ValueTypeField.TestField);
|
|
||||||
Assert.AreEqual(0, clone.ValueTypeField.TestProperty);
|
|
||||||
Assert.AreEqual(0, clone.ValueTypeProperty.TestField);
|
|
||||||
Assert.IsNull(clone.ReferenceTypeField);
|
|
||||||
|
|
||||||
if(isDeepClone) {
|
|
||||||
Assert.AreNotSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
|
|
||||||
Assert.AreNotSame(
|
|
||||||
original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
|
|
||||||
);
|
|
||||||
Assert.AreEqual(0, clone.ReferenceTypeProperty.TestField);
|
|
||||||
Assert.AreNotSame(
|
|
||||||
original.ReferenceTypeArrayProperty[1, 3][0],
|
|
||||||
clone.ReferenceTypeArrayProperty[1, 3][0]
|
|
||||||
);
|
|
||||||
Assert.AreNotSame(
|
|
||||||
original.ReferenceTypeArrayProperty[1, 3][2],
|
|
||||||
clone.ReferenceTypeArrayProperty[1, 3][2]
|
|
||||||
);
|
|
||||||
Assert.AreEqual(0, clone.ReferenceTypeArrayProperty[1, 3][0].TestField);
|
|
||||||
Assert.AreEqual(0, clone.ReferenceTypeArrayProperty[1, 3][2].TestField);
|
|
||||||
} else {
|
|
||||||
Assert.AreSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
|
|
||||||
Assert.AreSame(
|
|
||||||
original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Assert.AreEqual(original.TestField, clone.TestField);
|
|
||||||
Assert.AreEqual(original.ValueTypeField.TestField, clone.ValueTypeField.TestField);
|
|
||||||
Assert.AreEqual(original.ValueTypeField.TestProperty, clone.ValueTypeField.TestProperty);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ValueTypeProperty.TestField, clone.ValueTypeProperty.TestField
|
|
||||||
);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ReferenceTypeField.TestField, clone.ReferenceTypeField.TestField
|
|
||||||
);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ReferenceTypeField.TestProperty, clone.ReferenceTypeField.TestProperty
|
|
||||||
);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ReferenceTypeProperty.TestField, clone.ReferenceTypeProperty.TestField
|
|
||||||
);
|
|
||||||
|
|
||||||
if(isDeepClone) {
|
|
||||||
Assert.AreNotSame(original.ReferenceTypeField, clone.ReferenceTypeField);
|
|
||||||
Assert.AreNotSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
|
|
||||||
Assert.AreNotSame(
|
|
||||||
original.ReferenceTypeArrayField, clone.ReferenceTypeArrayField
|
|
||||||
);
|
|
||||||
Assert.AreNotSame(
|
|
||||||
original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
|
|
||||||
);
|
|
||||||
Assert.AreNotSame(
|
|
||||||
original.ReferenceTypeArrayProperty[1, 3][0],
|
|
||||||
clone.ReferenceTypeArrayProperty[1, 3][0]
|
|
||||||
);
|
|
||||||
Assert.AreNotSame(
|
|
||||||
original.ReferenceTypeArrayProperty[1, 3][2],
|
|
||||||
clone.ReferenceTypeArrayProperty[1, 3][2]
|
|
||||||
);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ReferenceTypeArrayProperty[1, 3][0].TestField,
|
|
||||||
clone.ReferenceTypeArrayProperty[1, 3][0].TestField
|
|
||||||
);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ReferenceTypeArrayProperty[1, 3][2].TestField,
|
|
||||||
clone.ReferenceTypeArrayProperty[1, 3][2].TestField
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
Assert.AreSame(original.ReferenceTypeField, clone.ReferenceTypeField);
|
|
||||||
Assert.AreSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
|
|
||||||
Assert.AreSame(
|
|
||||||
original.ReferenceTypeArrayField, clone.ReferenceTypeArrayField
|
|
||||||
);
|
|
||||||
Assert.AreSame(
|
|
||||||
original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Assert.AreEqual(original.TestProperty, clone.TestProperty);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ValueTypeProperty.TestProperty, clone.ValueTypeProperty.TestProperty
|
|
||||||
);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ReferenceTypeProperty.TestProperty, clone.ReferenceTypeProperty.TestProperty
|
|
||||||
);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ReferenceTypeArrayProperty[1, 3][0].TestProperty,
|
|
||||||
clone.ReferenceTypeArrayProperty[1, 3][0].TestProperty
|
|
||||||
);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ReferenceTypeArrayProperty[1, 3][2].TestProperty,
|
|
||||||
clone.ReferenceTypeArrayProperty[1, 3][2].TestProperty
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Verifies that a cloned object exhibits the expected state for the type of
|
|
||||||
/// clone that has been performed
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="original">Original instance the clone was created from</param>
|
|
||||||
/// <param name="clone">Cloned instance that will be checked for correctness</param>
|
|
||||||
/// <param name="isDeepClone">Whether the cloned instance is a deep clone</param>
|
|
||||||
/// <param name="isPropertyBasedClone">
|
|
||||||
/// Whether a property-based clone was performed
|
|
||||||
/// </param>
|
|
||||||
private static void verifyClone(
|
|
||||||
ref HierarchicalValueType original, ref HierarchicalValueType clone,
|
|
||||||
bool isDeepClone, bool isPropertyBasedClone
|
|
||||||
) {
|
|
||||||
if(isPropertyBasedClone) {
|
|
||||||
Assert.AreEqual(0, clone.TestField);
|
|
||||||
Assert.AreEqual(0, clone.ValueTypeField.TestField);
|
|
||||||
Assert.AreEqual(0, clone.ValueTypeField.TestProperty);
|
|
||||||
Assert.AreEqual(0, clone.ValueTypeProperty.TestField);
|
|
||||||
Assert.IsNull(clone.ReferenceTypeField);
|
|
||||||
|
|
||||||
if(isDeepClone) {
|
|
||||||
Assert.AreNotSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
|
|
||||||
Assert.AreNotSame(
|
|
||||||
original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
|
|
||||||
);
|
|
||||||
Assert.AreEqual(0, clone.ReferenceTypeProperty.TestField);
|
|
||||||
Assert.AreNotSame(
|
|
||||||
original.ReferenceTypeArrayProperty[1, 3][0],
|
|
||||||
clone.ReferenceTypeArrayProperty[1, 3][0]
|
|
||||||
);
|
|
||||||
Assert.AreNotSame(
|
|
||||||
original.ReferenceTypeArrayProperty[1, 3][2],
|
|
||||||
clone.ReferenceTypeArrayProperty[1, 3][2]
|
|
||||||
);
|
|
||||||
Assert.AreEqual(0, clone.ReferenceTypeArrayProperty[1, 3][0].TestField);
|
|
||||||
Assert.AreEqual(0, clone.ReferenceTypeArrayProperty[1, 3][2].TestField);
|
|
||||||
} else {
|
|
||||||
Assert.AreSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
|
|
||||||
Assert.AreSame(
|
|
||||||
original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Assert.AreEqual(original.TestField, clone.TestField);
|
|
||||||
Assert.AreEqual(original.ValueTypeField.TestField, clone.ValueTypeField.TestField);
|
|
||||||
Assert.AreEqual(original.ValueTypeField.TestProperty, clone.ValueTypeField.TestProperty);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ValueTypeProperty.TestField, clone.ValueTypeProperty.TestField
|
|
||||||
);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ReferenceTypeField.TestField, clone.ReferenceTypeField.TestField
|
|
||||||
);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ReferenceTypeField.TestProperty, clone.ReferenceTypeField.TestProperty
|
|
||||||
);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ReferenceTypeProperty.TestField, clone.ReferenceTypeProperty.TestField
|
|
||||||
);
|
|
||||||
|
|
||||||
if(isDeepClone) {
|
|
||||||
Assert.AreNotSame(original.ReferenceTypeField, clone.ReferenceTypeField);
|
|
||||||
Assert.AreNotSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
|
|
||||||
Assert.AreNotSame(
|
|
||||||
original.ReferenceTypeArrayField, clone.ReferenceTypeArrayField
|
|
||||||
);
|
|
||||||
Assert.AreNotSame(
|
|
||||||
original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
|
|
||||||
);
|
|
||||||
Assert.AreNotSame(
|
|
||||||
original.ReferenceTypeArrayProperty[1, 3][0],
|
|
||||||
clone.ReferenceTypeArrayProperty[1, 3][0]
|
|
||||||
);
|
|
||||||
Assert.AreNotSame(
|
|
||||||
original.ReferenceTypeArrayProperty[1, 3][2],
|
|
||||||
clone.ReferenceTypeArrayProperty[1, 3][2]
|
|
||||||
);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ReferenceTypeArrayProperty[1, 3][0].TestField,
|
|
||||||
clone.ReferenceTypeArrayProperty[1, 3][0].TestField
|
|
||||||
);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ReferenceTypeArrayProperty[1, 3][2].TestField,
|
|
||||||
clone.ReferenceTypeArrayProperty[1, 3][2].TestField
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
Assert.AreSame(original.ReferenceTypeField, clone.ReferenceTypeField);
|
|
||||||
Assert.AreSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
|
|
||||||
Assert.AreSame(
|
|
||||||
original.ReferenceTypeArrayField, clone.ReferenceTypeArrayField
|
|
||||||
);
|
|
||||||
Assert.AreSame(
|
|
||||||
original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Assert.AreEqual(original.TestProperty, clone.TestProperty);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ValueTypeProperty.TestProperty, clone.ValueTypeProperty.TestProperty
|
|
||||||
);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ReferenceTypeProperty.TestProperty, clone.ReferenceTypeProperty.TestProperty
|
|
||||||
);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ReferenceTypeArrayProperty[1, 3][0].TestProperty,
|
|
||||||
clone.ReferenceTypeArrayProperty[1, 3][0].TestProperty
|
|
||||||
);
|
|
||||||
Assert.AreEqual(
|
|
||||||
original.ReferenceTypeArrayProperty[1, 3][2].TestProperty,
|
|
||||||
clone.ReferenceTypeArrayProperty[1, 3][2].TestProperty
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Creates a value type with random data for testing</summary>
|
|
||||||
/// <returns>A new value type with random data</returns>
|
|
||||||
private static HierarchicalValueType createValueType() {
|
|
||||||
return new HierarchicalValueType() {
|
|
||||||
TestField = 123,
|
|
||||||
TestProperty = 321,
|
|
||||||
ReferenceTypeArrayField = new TestReferenceType[2, 4][] {
|
|
||||||
{
|
|
||||||
null, null, null, null
|
|
||||||
},
|
|
||||||
{
|
|
||||||
null, null, null,
|
|
||||||
new TestReferenceType[3] {
|
|
||||||
new TestReferenceType() { TestField = 101, TestProperty = 202 },
|
|
||||||
null,
|
|
||||||
new TestReferenceType() { TestField = 909, TestProperty = 808 }
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
ReferenceTypeArrayProperty = new TestReferenceType[2, 4][] {
|
|
||||||
{
|
|
||||||
null, null, null, null
|
|
||||||
},
|
|
||||||
{
|
|
||||||
null, null, null,
|
|
||||||
new TestReferenceType[3] {
|
|
||||||
new TestReferenceType() { TestField = 303, TestProperty = 404 },
|
|
||||||
null,
|
|
||||||
new TestReferenceType() { TestField = 707, TestProperty = 606 }
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
ValueTypeField = new TestValueType() {
|
|
||||||
TestField = 456,
|
|
||||||
TestProperty = 654
|
|
||||||
},
|
|
||||||
ValueTypeProperty = new TestValueType() {
|
|
||||||
TestField = 789,
|
|
||||||
TestProperty = 987,
|
|
||||||
},
|
|
||||||
ReferenceTypeField = new TestReferenceType() {
|
|
||||||
TestField = 135,
|
|
||||||
TestProperty = 531
|
|
||||||
},
|
|
||||||
ReferenceTypeProperty = new TestReferenceType() {
|
|
||||||
TestField = 246,
|
|
||||||
TestProperty = 642,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Creates a reference type with random data for testing</summary>
|
|
||||||
/// <returns>A new reference type with random data</returns>
|
|
||||||
private static HierarchicalReferenceType createReferenceType() {
|
|
||||||
return new HierarchicalReferenceType() {
|
|
||||||
TestField = 123,
|
|
||||||
TestProperty = 321,
|
|
||||||
ReferenceTypeArrayField = new TestReferenceType[2, 4][] {
|
|
||||||
{
|
|
||||||
null, null, null, null
|
|
||||||
},
|
|
||||||
{
|
|
||||||
null, null, null,
|
|
||||||
new TestReferenceType[3] {
|
|
||||||
new TestReferenceType() { TestField = 101, TestProperty = 202 },
|
|
||||||
null,
|
|
||||||
new TestReferenceType() { TestField = 909, TestProperty = 808 }
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
ReferenceTypeArrayProperty = new TestReferenceType[2, 4][] {
|
|
||||||
{
|
|
||||||
null, null, null, null
|
|
||||||
},
|
|
||||||
{
|
|
||||||
null, null, null,
|
|
||||||
new TestReferenceType[3] {
|
|
||||||
new TestReferenceType() { TestField = 303, TestProperty = 404 },
|
|
||||||
null,
|
|
||||||
new TestReferenceType() { TestField = 707, TestProperty = 606 }
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
ValueTypeField = new TestValueType() {
|
|
||||||
TestField = 456,
|
|
||||||
TestProperty = 654
|
|
||||||
},
|
|
||||||
ValueTypeProperty = new TestValueType() {
|
|
||||||
TestField = 789,
|
|
||||||
TestProperty = 987,
|
|
||||||
},
|
|
||||||
ReferenceTypeField = new TestReferenceType() {
|
|
||||||
TestField = 135,
|
|
||||||
TestProperty = 531
|
|
||||||
},
|
|
||||||
ReferenceTypeProperty = new TestReferenceType() {
|
|
||||||
TestField = 246,
|
|
||||||
TestProperty = 642,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,13 @@ using System.Reflection;
|
||||||
namespace Nuclex.Support.Cloning {
|
namespace Nuclex.Support.Cloning {
|
||||||
|
|
||||||
/// <summary>Clones objects using reflection</summary>
|
/// <summary>Clones objects using reflection</summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>
|
||||||
|
/// This type of cloning is a lot faster than cloning by serialization and
|
||||||
|
/// incurs no set-up cost, but requires cloned types to provide a default
|
||||||
|
/// constructor in order to work.
|
||||||
|
/// </para>
|
||||||
|
/// </remarks>
|
||||||
public class ReflectionCloner : ICloneFactory {
|
public class ReflectionCloner : ICloneFactory {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
126
Source/Cloning/SerializationCloner.Test.cs
Normal file
126
Source/Cloning/SerializationCloner.Test.cs
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
#region CPL License
|
||||||
|
/*
|
||||||
|
Nuclex Framework
|
||||||
|
Copyright (C) 2002-2010 Nuclex Development Labs
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the IBM Common Public License as
|
||||||
|
published by the IBM Corporation; either version 1.0 of the
|
||||||
|
License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
IBM Common Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the IBM Common Public
|
||||||
|
License along with this library
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#if UNITTEST
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace Nuclex.Support.Cloning {
|
||||||
|
|
||||||
|
/// <summary>Unit Test for the reflection-based cloner</summary>
|
||||||
|
[TestFixture]
|
||||||
|
public class SerializationClonerTest : CloneFactoryTest {
|
||||||
|
|
||||||
|
/// <summary>Initializes a new unit test suite for the reflection cloner</summary>
|
||||||
|
public SerializationClonerTest() {
|
||||||
|
this.cloneFactory = new SerializationCloner();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Verifies that clones of primitive types can be created</summary>
|
||||||
|
[Test]
|
||||||
|
public void PrimitiveTypesCanBeCloned() {
|
||||||
|
int original = 12345;
|
||||||
|
int clone = this.cloneFactory.DeepClone(original, false);
|
||||||
|
Assert.AreEqual(original, clone);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Verifies that deep clones of arrays can be made</summary>
|
||||||
|
[Test]
|
||||||
|
public void DeepClonesOfArraysCanBeMade() {
|
||||||
|
var original = new TestReferenceType[] {
|
||||||
|
new TestReferenceType() { TestField = 123, TestProperty = 456 }
|
||||||
|
};
|
||||||
|
TestReferenceType[] clone = this.cloneFactory.DeepClone(original, false);
|
||||||
|
|
||||||
|
Assert.AreNotSame(original[0], clone[0]);
|
||||||
|
Assert.AreEqual(original[0].TestField, clone[0].TestField);
|
||||||
|
Assert.AreEqual(original[0].TestProperty, clone[0].TestProperty);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Verifies that deep clones of a generic list can be made</summary>
|
||||||
|
[Test]
|
||||||
|
public void GenericListsCanBeCloned() {
|
||||||
|
var original = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
|
||||||
|
List<int> clone = this.cloneFactory.DeepClone(original, false);
|
||||||
|
|
||||||
|
CollectionAssert.AreEqual(original, clone);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Verifies that deep clones of a generic dictionary can be made</summary>
|
||||||
|
[Test]
|
||||||
|
public void GenericDictionariesCanBeCloned() {
|
||||||
|
var original = new Dictionary<int, string>();
|
||||||
|
original.Add(1, "one");
|
||||||
|
Dictionary<int, string> clone = this.cloneFactory.DeepClone(original, false);
|
||||||
|
|
||||||
|
Assert.AreEqual("one", clone[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that a field-based deep clone of a value type can be performed
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void DeepFieldBasedClonesOfValueTypesCanBeMade() {
|
||||||
|
HierarchicalValueType original = CreateValueType();
|
||||||
|
HierarchicalValueType clone = this.cloneFactory.DeepClone(original, false);
|
||||||
|
VerifyClone(ref original, ref clone, isDeepClone: true, isPropertyBasedClone: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that a field-based deep clone of a reference type can be performed
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void DeepFieldBasedClonesOfReferenceTypesCanBeMade() {
|
||||||
|
HierarchicalReferenceType original = CreateReferenceType();
|
||||||
|
HierarchicalReferenceType clone = this.cloneFactory.DeepClone(original, false);
|
||||||
|
VerifyClone(original, clone, isDeepClone: true, isPropertyBasedClone: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that a property-based deep clone of a value type can be performed
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void DeepPropertyBasedClonesOfValueTypesCanBeMade() {
|
||||||
|
HierarchicalValueType original = CreateValueType();
|
||||||
|
HierarchicalValueType clone = this.cloneFactory.DeepClone(original, true);
|
||||||
|
VerifyClone(ref original, ref clone, isDeepClone: true, isPropertyBasedClone: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that a property-based deep clone of a reference type can be performed
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void DeepPropertyBasedClonesOfReferenceTypesCanBeMade() {
|
||||||
|
HierarchicalReferenceType original = CreateReferenceType();
|
||||||
|
HierarchicalReferenceType clone = this.cloneFactory.DeepClone(original, true);
|
||||||
|
VerifyClone(original, clone, isDeepClone: true, isPropertyBasedClone: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Clone factory being tested</summary>
|
||||||
|
private ICloneFactory cloneFactory;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Nuclex.Support.Cloning
|
||||||
|
|
||||||
|
#endif // UNITTEST
|
288
Source/Cloning/SerializationCloner.cs
Normal file
288
Source/Cloning/SerializationCloner.cs
Normal file
|
@ -0,0 +1,288 @@
|
||||||
|
#region CPL License
|
||||||
|
/*
|
||||||
|
Nuclex Framework
|
||||||
|
Copyright (C) 2002-2010 Nuclex Development Labs
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the IBM Common Public License as
|
||||||
|
published by the IBM Corporation; either version 1.0 of the
|
||||||
|
License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
IBM Common Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the IBM Common Public
|
||||||
|
License along with this library
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
|
using System.Runtime.Serialization.Formatters.Binary;
|
||||||
|
|
||||||
|
namespace Nuclex.Support.Cloning {
|
||||||
|
|
||||||
|
/// <summary>Clones objects via serialization</summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>
|
||||||
|
/// This type of cloning uses the binary formatter to persist the state of
|
||||||
|
/// an object and then restores it into a clone. It has the advantage of even
|
||||||
|
/// working with types that don't provide a default constructor, but is
|
||||||
|
/// terribly slow.
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// Inspired by the "A Generic Method for Deep Cloning in C# 3.0" article
|
||||||
|
/// on CodeProject: http://www.codeproject.com/KB/cs/generic_deep_cloning.aspx
|
||||||
|
/// </para>
|
||||||
|
/// </remarks>
|
||||||
|
public class SerializationCloner : ICloneFactory {
|
||||||
|
|
||||||
|
#region class StaticSurrogateSelector
|
||||||
|
|
||||||
|
/// <summary>Selects a static surrogate for any non-primitive types</summary>
|
||||||
|
private class StaticSurrogateSelector : ISurrogateSelector {
|
||||||
|
|
||||||
|
/// <summary>Initializes a new static surrogate selector</summary>
|
||||||
|
/// <param name="staticSurrogate">Surrogate that will be selected</param>
|
||||||
|
public StaticSurrogateSelector(ISerializationSurrogate staticSurrogate) {
|
||||||
|
this.staticSurrogate = staticSurrogate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the next selector to escalate to if this one can't provide a surrogate
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="selector">Selector to escalate to</param>
|
||||||
|
public void ChainSelector(ISurrogateSelector selector) {
|
||||||
|
this.chainedSelector = selector;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the selector this one will escalate to if it can't provide a surrogate
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The selector this one will escalate to</returns>
|
||||||
|
public ISurrogateSelector GetNextSelector() {
|
||||||
|
return this.chainedSelector;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Attempts to provides a surrogate for the specified type</summary>
|
||||||
|
/// <param name="type">Type a surrogate will be provided for</param>
|
||||||
|
/// <param name="context">Context </param>
|
||||||
|
/// <param name="selector"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public ISerializationSurrogate GetSurrogate(
|
||||||
|
Type type, StreamingContext context, out ISurrogateSelector selector
|
||||||
|
) {
|
||||||
|
if(type.IsPrimitive || type.IsArray || (type == typeof(string))) {
|
||||||
|
if(this.chainedSelector == null) {
|
||||||
|
selector = null;
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return this.chainedSelector.GetSurrogate(type, context, out selector);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
selector = this;
|
||||||
|
return this.staticSurrogate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Surrogate the that will be selected for any non-primitive types</summary>
|
||||||
|
private ISerializationSurrogate staticSurrogate;
|
||||||
|
/// <summary>Surrogate selector to escalate to if no surrogate can be provided</summary>
|
||||||
|
private ISurrogateSelector chainedSelector;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // class StaticSurrogateSelector
|
||||||
|
|
||||||
|
#region class FieldSerializationSurrogate
|
||||||
|
|
||||||
|
/// <summary>Serializes a type based on its fields</summary>
|
||||||
|
private class FieldSerializationSurrogate : ISerializationSurrogate {
|
||||||
|
|
||||||
|
/// <summary>Extracts the data to be serialized from an object</summary>
|
||||||
|
/// <param name="objectToSerialize">Object that is being serialized</param>
|
||||||
|
/// <param name="info">Stores the serialized informations</param>
|
||||||
|
/// <param name="context">
|
||||||
|
/// Provides additional informations about the serialization process
|
||||||
|
/// </param>
|
||||||
|
public void GetObjectData(
|
||||||
|
object objectToSerialize,
|
||||||
|
SerializationInfo info,
|
||||||
|
StreamingContext context
|
||||||
|
) {
|
||||||
|
Type originalType = objectToSerialize.GetType();
|
||||||
|
|
||||||
|
FieldInfo[] fieldInfos = originalType.GetFields(
|
||||||
|
BindingFlags.Public | BindingFlags.NonPublic |
|
||||||
|
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
||||||
|
);
|
||||||
|
for(int index = 0; index < fieldInfos.Length; ++index) {
|
||||||
|
FieldInfo fieldInfo = fieldInfos[index];
|
||||||
|
info.AddValue(fieldInfo.Name, fieldInfo.GetValue(objectToSerialize));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Reinserts saved data into a deserializd object</summary>
|
||||||
|
/// <param name="deserializedObject">Object the saved data will be inserted into</param>
|
||||||
|
/// <param name="info">Contains the serialized informations</param>
|
||||||
|
/// <param name="context">
|
||||||
|
/// Provides additional informations about the serialization process
|
||||||
|
/// </param>
|
||||||
|
/// <param name="selector">Surrogate selector that specified this surrogate</param>
|
||||||
|
/// <returns>The deserialized object</returns>
|
||||||
|
public object SetObjectData(
|
||||||
|
object deserializedObject,
|
||||||
|
SerializationInfo info,
|
||||||
|
StreamingContext context,
|
||||||
|
ISurrogateSelector selector
|
||||||
|
) {
|
||||||
|
Type originalType = deserializedObject.GetType();
|
||||||
|
|
||||||
|
FieldInfo[] fieldInfos = originalType.GetFields(
|
||||||
|
BindingFlags.Public | BindingFlags.NonPublic |
|
||||||
|
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
||||||
|
);
|
||||||
|
for(int index = 0; index < fieldInfos.Length; ++index) {
|
||||||
|
FieldInfo fieldInfo = fieldInfos[index];
|
||||||
|
fieldInfo.SetValue(deserializedObject, info.GetValue(fieldInfo.Name, fieldInfo.FieldType));
|
||||||
|
}
|
||||||
|
|
||||||
|
return deserializedObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // class FieldSerializationSurrogate
|
||||||
|
|
||||||
|
#region class PropertySerializationSurrogate
|
||||||
|
|
||||||
|
/// <summary>Serializes a type based on its properties</summary>
|
||||||
|
private class PropertySerializationSurrogate : ISerializationSurrogate {
|
||||||
|
|
||||||
|
/// <summary>Extracts the data to be serialized from an object</summary>
|
||||||
|
/// <param name="objectToSerialize">Object that is being serialized</param>
|
||||||
|
/// <param name="info">Stores the serialized informations</param>
|
||||||
|
/// <param name="context">
|
||||||
|
/// Provides additional informations about the serialization process
|
||||||
|
/// </param>
|
||||||
|
public void GetObjectData(
|
||||||
|
object objectToSerialize,
|
||||||
|
SerializationInfo info,
|
||||||
|
StreamingContext context
|
||||||
|
) {
|
||||||
|
Type originalType = objectToSerialize.GetType();
|
||||||
|
|
||||||
|
PropertyInfo[] propertyInfos = originalType.GetProperties(
|
||||||
|
BindingFlags.Public | BindingFlags.NonPublic |
|
||||||
|
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
||||||
|
);
|
||||||
|
for(int index = 0; index < propertyInfos.Length; ++index) {
|
||||||
|
PropertyInfo propertyInfo = propertyInfos[index];
|
||||||
|
info.AddValue(propertyInfo.Name, propertyInfo.GetValue(objectToSerialize, null));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Reinserts saved data into a deserializd object</summary>
|
||||||
|
/// <param name="deserializedObject">Object the saved data will be inserted into</param>
|
||||||
|
/// <param name="info">Contains the serialized informations</param>
|
||||||
|
/// <param name="context">
|
||||||
|
/// Provides additional informations about the serialization process
|
||||||
|
/// </param>
|
||||||
|
/// <param name="selector">Surrogate selector that specified this surrogate</param>
|
||||||
|
/// <returns>The deserialized object</returns>
|
||||||
|
public object SetObjectData(
|
||||||
|
object deserializedObject,
|
||||||
|
SerializationInfo info,
|
||||||
|
StreamingContext context,
|
||||||
|
ISurrogateSelector selector
|
||||||
|
) {
|
||||||
|
Type originalType = deserializedObject.GetType();
|
||||||
|
|
||||||
|
PropertyInfo[] propertyInfos = originalType.GetProperties(
|
||||||
|
BindingFlags.Public | BindingFlags.NonPublic |
|
||||||
|
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
||||||
|
);
|
||||||
|
for(int index = 0; index < propertyInfos.Length; ++index) {
|
||||||
|
PropertyInfo propertyInfo = propertyInfos[index];
|
||||||
|
propertyInfo.SetValue(
|
||||||
|
deserializedObject,
|
||||||
|
info.GetValue(propertyInfo.Name, propertyInfo.PropertyType),
|
||||||
|
null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return deserializedObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // class PropertySerializationSurrogate
|
||||||
|
|
||||||
|
/// <summary>Initializes a new serialization-based cloner</summary>
|
||||||
|
public SerializationCloner() {
|
||||||
|
var fieldSurrogateSelector = new SurrogateSelector();
|
||||||
|
fieldSurrogateSelector.ChainSelector(
|
||||||
|
new StaticSurrogateSelector(new FieldSerializationSurrogate())
|
||||||
|
);
|
||||||
|
this.fieldBasedFormatter = new BinaryFormatter(
|
||||||
|
fieldSurrogateSelector, new StreamingContext(StreamingContextStates.All)
|
||||||
|
);
|
||||||
|
|
||||||
|
var propertySurrogateSelector = new SurrogateSelector();
|
||||||
|
propertySurrogateSelector.ChainSelector(
|
||||||
|
new StaticSurrogateSelector(new PropertySerializationSurrogate())
|
||||||
|
);
|
||||||
|
this.propertyBasedFormatter = new BinaryFormatter(
|
||||||
|
propertySurrogateSelector, new StreamingContext(StreamingContextStates.All)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a deep clone of the specified object, also creating clones of all
|
||||||
|
/// child objects being referenced
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TCloned">Type of the object that will be cloned</typeparam>
|
||||||
|
/// <param name="objectToClone">Object that will be cloned</param>
|
||||||
|
/// <param name="usePropertyBasedClone">
|
||||||
|
/// Whether to clone the object based on its properties only
|
||||||
|
/// </param>
|
||||||
|
/// <returns>A deep clone of the provided object</returns>
|
||||||
|
public TCloned DeepClone<TCloned>(TCloned objectToClone, bool usePropertyBasedClone) {
|
||||||
|
using(var memoryStream = new MemoryStream()) {
|
||||||
|
if(usePropertyBasedClone) {
|
||||||
|
this.propertyBasedFormatter.Serialize(memoryStream, objectToClone);
|
||||||
|
memoryStream.Position = 0;
|
||||||
|
return (TCloned)this.propertyBasedFormatter.Deserialize(memoryStream);
|
||||||
|
} else {
|
||||||
|
this.fieldBasedFormatter.Serialize(memoryStream, objectToClone);
|
||||||
|
memoryStream.Position = 0;
|
||||||
|
return (TCloned)this.fieldBasedFormatter.Deserialize(memoryStream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a shallow clone of the specified object, reusing any referenced objects
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TCloned">Type of the object that will be cloned</typeparam>
|
||||||
|
/// <param name="objectToClone">Object that will be cloned</param>
|
||||||
|
/// <param name="usePropertyBasedClone">
|
||||||
|
/// Whether to clone the object based on its properties only
|
||||||
|
/// </param>
|
||||||
|
/// <returns>A shallow clone of the provided object</returns>
|
||||||
|
public TCloned ShallowClone<TCloned>(TCloned objectToClone, bool usePropertyBasedClone) {
|
||||||
|
throw new NotSupportedException("The serialization cloner cannot create shallow clones");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Serializes objects by storing their fields</summary>
|
||||||
|
private BinaryFormatter fieldBasedFormatter;
|
||||||
|
/// <summary>Serializes objects by storing their properties</summary>
|
||||||
|
private BinaryFormatter propertyBasedFormatter;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Nuclex.Support.Cloning
|
Loading…
Reference in New Issue
Block a user