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

View File

@ -285,23 +285,35 @@ namespace Nuclex.Support.Cloning {
Expression.Assign(Expression.ArrayAccess(clone, indexes), clonedElement)
);
} else {
ParameterExpression clonedElement = Expression.Variable(elementType);
loopVariables.Add(clonedElement);
nestedTransferExpressions.Add(
Expression.Assign(clonedElement, originalElement)
// Complex types are cloned by checking their actual, concrete type (fields
// may be typed to an interface or base class) and requesting a cloner for that
// type during runtime
MethodInfo getOrCreateClonerMethodInfo = typeof(ExpressionTreeCloner).GetMethod(
"getOrCreateDeepFieldBasedCloner",
BindingFlags.NonPublic | BindingFlags.Static
);
MethodInfo getTypeMethodInfo = typeof(object).GetMethod("GetType");
MethodInfo invokeMethodInfo = typeof(Func<object, object>).GetMethod("Invoke");
//generateComplexTypeTransferExpressions(
// elementType,
// originalElement,
// clonedElement,
// loopVariables,
// loopExpressions
//);
// Generate expressions to do this:
// clone.SomeField = getOrCreateDeepFieldBasedCloner(
// original.SomeField.GetType()
// ).Invoke(original.SomeField);
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();
}
/// <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
/// <summary>Compiled cloners that perform shallow clone operations</summary>