Arrays of reference types can now be cloned, too - deep expression tree cloner is finished!

git-svn-id: file:///srv/devel/repo-conversion/nusu@240 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2012-02-08 11:12:44 +00:00
parent e57140579c
commit e7d1c9720b
2 changed files with 36 additions and 39 deletions

View File

@ -77,6 +77,7 @@ namespace Nuclex.Support.Cloning {
Assert.AreSame(original[0], clone[0]); Assert.AreSame(original[0], clone[0]);
} }
#endif #endif
/// <summary>Verifies that deep clones of arrays can be made</summary> /// <summary>Verifies that deep clones of arrays can be made</summary>
[Test] [Test]
public void DeepClonesOfArraysCanBeMade() { public void DeepClonesOfArraysCanBeMade() {
@ -87,11 +88,11 @@ namespace Nuclex.Support.Cloning {
}; };
TestReferenceType[,] clone = this.cloneFactory.DeepClone(original, false); TestReferenceType[,] clone = this.cloneFactory.DeepClone(original, false);
//Assert.AreNotSame(original[0, 0], clone[0, 0]); Assert.AreNotSame(original[0, 0], clone[0, 0]);
//Assert.AreEqual(original[0,0].TestField, clone[0,0].TestField); Assert.AreEqual(original[0, 0].TestField, clone[0, 0].TestField);
//Assert.AreEqual(original[0,0].TestProperty, clone[0,0].TestProperty); Assert.AreEqual(original[0, 0].TestProperty, clone[0, 0].TestProperty);
} }
#if false
/// <summary>Verifies that deep clones of a generic list can be made</summary> /// <summary>Verifies that deep clones of a generic list can be made</summary>
[Test] [Test]
public void GenericListsCanBeCloned() { public void GenericListsCanBeCloned() {
@ -111,6 +112,7 @@ namespace Nuclex.Support.Cloning {
Assert.AreEqual("one", clone[1]); Assert.AreEqual("one", clone[1]);
} }
#if false
/// <summary> /// <summary>
/// Verifies that a field-based shallow clone of a value type can be performed /// Verifies that a field-based shallow clone of a value type can be performed
/// </summary> /// </summary>
@ -138,7 +140,7 @@ namespace Nuclex.Support.Cloning {
public void DeepFieldBasedClonesOfValueTypesCanBeMade() { public void DeepFieldBasedClonesOfValueTypesCanBeMade() {
HierarchicalValueType original = CreateValueType(); HierarchicalValueType original = CreateValueType();
HierarchicalValueType clone = this.cloneFactory.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>
@ -148,7 +150,7 @@ namespace Nuclex.Support.Cloning {
public void DeepFieldBasedClonesOfReferenceTypesCanBeMade() { public void DeepFieldBasedClonesOfReferenceTypesCanBeMade() {
HierarchicalReferenceType original = CreateReferenceType(); HierarchicalReferenceType original = CreateReferenceType();
HierarchicalReferenceType clone = this.cloneFactory.DeepClone(original, false); HierarchicalReferenceType clone = this.cloneFactory.DeepClone(original, false);
//VerifyClone(original, clone, isDeepClone: true, isPropertyBasedClone: false); VerifyClone(original, clone, isDeepClone: true, isPropertyBasedClone: false);
} }
#if false #if false
@ -171,7 +173,9 @@ namespace Nuclex.Support.Cloning {
HierarchicalReferenceType clone = this.cloneFactory.ShallowClone(original, true); HierarchicalReferenceType clone = this.cloneFactory.ShallowClone(original, true);
VerifyClone(original, clone, isDeepClone: false, isPropertyBasedClone: true); VerifyClone(original, clone, isDeepClone: false, isPropertyBasedClone: true);
} }
#endif
#if false
/// <summary> /// <summary>
/// Verifies that a property-based deep clone of a value type can be performed /// Verifies that a property-based deep clone of a value type can be performed
/// </summary> /// </summary>

View File

@ -285,23 +285,35 @@ namespace Nuclex.Support.Cloning {
Expression.Assign(Expression.ArrayAccess(clone, indexes), clonedElement) Expression.Assign(Expression.ArrayAccess(clone, indexes), clonedElement)
); );
} else { } else {
ParameterExpression clonedElement = Expression.Variable(elementType); // Complex types are cloned by checking their actual, concrete type (fields
loopVariables.Add(clonedElement); // may be typed to an interface or base class) and requesting a cloner for that
// type during runtime
nestedTransferExpressions.Add( MethodInfo getOrCreateClonerMethodInfo = typeof(ExpressionTreeCloner).GetMethod(
Expression.Assign(clonedElement, originalElement) "getOrCreateDeepFieldBasedCloner",
BindingFlags.NonPublic | BindingFlags.Static
); );
MethodInfo getTypeMethodInfo = typeof(object).GetMethod("GetType");
MethodInfo invokeMethodInfo = typeof(Func<object, object>).GetMethod("Invoke");
//generateComplexTypeTransferExpressions( // Generate expressions to do this:
// elementType, // clone.SomeField = getOrCreateDeepFieldBasedCloner(
// originalElement, // original.SomeField.GetType()
// clonedElement, // ).Invoke(original.SomeField);
// loopVariables,
// loopExpressions
//);
nestedTransferExpressions.Add( nestedTransferExpressions.Add(
Expression.Assign(Expression.ArrayAccess(clone, indexes), clonedElement) Expression.Assign(
Expression.ArrayAccess(clone, indexes),
Expression.Convert(
Expression.Call(
Expression.Call(
getOrCreateClonerMethodInfo,
Expression.Call(originalElement, getTypeMethodInfo)
),
invokeMethodInfo,
originalElement
),
elementType
)
)
); );
} }
@ -637,25 +649,6 @@ namespace Nuclex.Support.Cloning {
throw new NotImplementedException(); throw new NotImplementedException();
} }
/// <summary>
/// Compiles a method that copies the state of one object into another object
/// </summary>
/// <typeparam name="TCloned">Type of object whose state will be copied</typeparam>
/// <param name="deepClone">Whether to create clones of the referenced objects</param>
/// <returns>A method that copies the state from one object into another object</returns>
public static ReferenceAction<TCloned, TCloned> CreateValueCopier<TCloned>(bool deepClone)
where TCloned : struct {
throw new NotImplementedException();
}
/// <summary>Compiles a method that creates a clone of an object</summary>
/// <typeparam name="TCloned">Type of object that will be cloned</typeparam>
/// <param name="deepClone">Whether to create clones of the referenced objects</param>
/// <returns>A method that clones an object of the provided type</returns>
public static Func<TCloned, TCloned> CreateCloner<TCloned>(bool deepClone)
where TCloned : class, new() {
throw new NotImplementedException();
}
#endif #endif
/// <summary>Compiled cloners that perform shallow clone operations</summary> /// <summary>Compiled cloners that perform shallow clone operations</summary>