Expression tree cloner now clones nested reference types

git-svn-id: file:///srv/devel/repo-conversion/nusu@233 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2012-02-05 13:12:18 +00:00
parent 79063f59c1
commit 3267e399ee
2 changed files with 471 additions and 384 deletions

View File

@ -27,43 +27,43 @@ using NUnit.Framework;
namespace Nuclex.Support.Cloning { namespace Nuclex.Support.Cloning {
/// <summary>Unit Test for the expression tree-based cloner</summary> /// <summary>Unit Test for the expression tree-based cloner</summary>
[TestFixture] [TestFixture]
public class ExpressionTreeClonerTest : CloneFactoryTest { public class ExpressionTreeClonerTest : CloneFactoryTest {
/// <summary>Initializes a new unit test suite for the reflection cloner</summary> /// <summary>Initializes a new unit test suite for the reflection cloner</summary>
public ExpressionTreeClonerTest() { public ExpressionTreeClonerTest() {
this.cloneFactory = new ExpressionTreeCloner(); this.cloneFactory = new ExpressionTreeCloner();
} }
/// <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 = this.cloneFactory.DeepClone(original, false); int clone = this.cloneFactory.DeepClone(original, false);
Assert.AreEqual(original, clone); Assert.AreEqual(original, clone);
} }
/// <summary>Verifies that shallow clones of arrays can be made</summary> /// <summary>Verifies that shallow clones of arrays can be made</summary>
[Test] [Test]
public void ReferenceTypesCanBeCloned() { public void ReferenceTypesCanBeCloned() {
var original = new TestReferenceType() { TestField = 123, TestProperty = 456 }; var original = new TestReferenceType() { TestField = 123, TestProperty = 456 };
TestReferenceType clone = this.cloneFactory.DeepClone(original, false); TestReferenceType clone = this.cloneFactory.DeepClone(original, false);
Assert.AreNotSame(original, clone); Assert.AreNotSame(original, clone);
Assert.AreEqual(original.TestField, clone.TestField); Assert.AreEqual(original.TestField, clone.TestField);
Assert.AreEqual(original.TestProperty, clone.TestProperty); Assert.AreEqual(original.TestProperty, clone.TestProperty);
} }
/// <summary>Verifies that shallow clones of arrays can be made</summary> /// <summary>Verifies that shallow clones of arrays can be made</summary>
[Test] [Test]
public void PrimitiveArraysCanBeCloned() { public void PrimitiveArraysCanBeCloned() {
var original = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var original = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] clone = this.cloneFactory.DeepClone(original, false); int[] clone = this.cloneFactory.DeepClone(original, false);
Assert.AreNotSame(original, clone); Assert.AreNotSame(original, clone);
CollectionAssert.AreEqual(original, clone); CollectionAssert.AreEqual(original, clone);
} }
#if false #if false
/// <summary>Verifies that shallow clones of arrays can be made</summary> /// <summary>Verifies that shallow clones of arrays can be made</summary>
@ -77,20 +77,20 @@ 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() {
var original = new TestReferenceType[,] { var original = new TestReferenceType[,] {
{ {
new TestReferenceType() { TestField = 123, TestProperty = 456 } new TestReferenceType() { TestField = 123, TestProperty = 456 }
} }
}; };
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 #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]
@ -130,15 +130,15 @@ namespace Nuclex.Support.Cloning {
HierarchicalReferenceType clone = this.cloneFactory.ShallowClone(original, false); HierarchicalReferenceType clone = this.cloneFactory.ShallowClone(original, false);
VerifyClone(original, clone, isDeepClone: false, isPropertyBasedClone: false); VerifyClone(original, clone, isDeepClone: false, isPropertyBasedClone: false);
} }
#endif
/// <summary> /// <summary>
/// Verifies that a field-based deep clone of a value type can be performed /// Verifies that a field-based deep clone of a value type can be performed
/// </summary> /// </summary>
[Test] [Test]
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,9 +148,10 @@ 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
/// <summary> /// <summary>
/// Verifies that a property-based shallow clone of a value type can be performed /// Verifies that a property-based shallow clone of a value type can be performed
/// </summary> /// </summary>
@ -192,10 +193,10 @@ namespace Nuclex.Support.Cloning {
} }
#endif #endif
/// <summary>Clone factory being tested</summary> /// <summary>Clone factory being tested</summary>
private ICloneFactory cloneFactory; private ICloneFactory cloneFactory;
} }
} // namespace Nuclex.Support.Cloning } // namespace Nuclex.Support.Cloning

View File

@ -22,369 +22,455 @@ License along with this library
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Reflection;
using System.Linq.Expressions;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace Nuclex.Support.Cloning { namespace Nuclex.Support.Cloning {
/// <summary>An action that takes its arguments as references to a structure</summary> /// <summary>An action that takes its arguments as references to a structure</summary>
/// <typeparam name="TFirst">Type of the first argument to the method</typeparam> /// <typeparam name="TFirst">Type of the first argument to the method</typeparam>
/// <typeparam name="TSecond">Type of the second argument to the method</typeparam> /// <typeparam name="TSecond">Type of the second argument to the method</typeparam>
/// <param name="first">First argument to the method</param> /// <param name="first">First argument to the method</param>
/// <param name="second">Second argument to the method</param> /// <param name="second">Second argument to the method</param>
public delegate void ReferenceAction<TFirst, TSecond>(ref TFirst first, ref TSecond second) public delegate void ReferenceAction<TFirst, TSecond>(ref TFirst first, ref TSecond second)
where TFirst : struct where TFirst : struct
where TSecond : struct; where TSecond : struct;
/// <summary> /// <summary>
/// Cloning factory which uses expression trees to improve performance when cloning /// Cloning factory which uses expression trees to improve performance when cloning
/// is a high-frequency action. /// is a high-frequency action.
/// </summary> /// </summary>
public class ExpressionTreeCloner : ICloneFactory { public class ExpressionTreeCloner : ICloneFactory {
/// <summary>Initializes the static members of the expression tree cloner</summary> /// <summary>Initializes the static members of the expression tree cloner</summary>
static ExpressionTreeCloner() { static ExpressionTreeCloner() {
shallowCloners = new ConcurrentDictionary<Type, Delegate>(); shallowCloners = new ConcurrentDictionary<Type, Func<object, object>>();
deepCloners = new ConcurrentDictionary<Type, Delegate>(); deepCloners = new ConcurrentDictionary<Type, Func<object, object>>();
} }
/// <summary> /// <summary>
/// Creates a deep clone of the specified object, also creating clones of all /// Creates a deep clone of the specified object, also creating clones of all
/// child objects being referenced /// child objects being referenced
/// </summary> /// </summary>
/// <typeparam name="TCloned">Type of the object that will be cloned</typeparam> /// <typeparam name="TCloned">Type of the object that will be cloned</typeparam>
/// <param name="objectToClone">Object that will be cloned</param> /// <param name="objectToClone">Object that will be cloned</param>
/// <param name="usePropertyBasedClone"> /// <param name="usePropertyBasedClone">
/// Whether to clone the object based on its properties only /// Whether to clone the object based on its properties only
/// </param> /// </param>
/// <returns>A deep clone of the provided object</returns> /// <returns>A deep clone of the provided object</returns>
public static TCloned DeepClone<TCloned>( public static TCloned DeepClone<TCloned>(
TCloned objectToClone, bool usePropertyBasedClone TCloned objectToClone, bool usePropertyBasedClone
) { ) {
if(usePropertyBasedClone) { object objectToCloneAsObject = objectToClone;
throw new NotImplementedException("Not implemented yet"); if(objectToCloneAsObject == null) {
} else { return default(TCloned);
Func<TCloned, TCloned> cloner = getOrCreateDeepFieldBasedCloner<TCloned>(); }
return cloner(objectToClone);
}
}
/// <summary> if(usePropertyBasedClone) {
/// Creates a shallow clone of the specified object, reusing any referenced objects throw new NotImplementedException("Not implemented yet");
/// </summary> } else {
/// <typeparam name="TCloned">Type of the object that will be cloned</typeparam> Func<object, object> cloner = getOrCreateDeepFieldBasedCloner(typeof(TCloned));
/// <param name="objectToClone">Object that will be cloned</param> return (TCloned)cloner(objectToCloneAsObject);
/// <param name="usePropertyBasedClone"> }
/// Whether to clone the object based on its properties only }
/// </param>
/// <returns>A shallow clone of the provided object</returns>
public static TCloned ShallowClone<TCloned>(
TCloned objectToClone, bool usePropertyBasedClone
) {
throw new NotImplementedException("Not implemented yet");
}
/// <summary> /// <summary>
/// Creates a deep clone of the specified object, also creating clones of all /// Creates a shallow clone of the specified object, reusing any referenced objects
/// child objects being referenced /// </summary>
/// </summary> /// <typeparam name="TCloned">Type of the object that will be cloned</typeparam>
/// <typeparam name="TCloned">Type of the object that will be cloned</typeparam> /// <param name="objectToClone">Object that will be cloned</param>
/// <param name="objectToClone">Object that will be cloned</param> /// <param name="usePropertyBasedClone">
/// <param name="usePropertyBasedClone"> /// Whether to clone the object based on its properties only
/// Whether to clone the object based on its properties only /// </param>
/// </param> /// <returns>A shallow clone of the provided object</returns>
/// <returns>A deep clone of the provided object</returns> public static TCloned ShallowClone<TCloned>(
TCloned ICloneFactory.DeepClone<TCloned>( TCloned objectToClone, bool usePropertyBasedClone
TCloned objectToClone, bool usePropertyBasedClone ) {
) { throw new NotImplementedException("Not implemented yet");
return ExpressionTreeCloner.DeepClone<TCloned>(objectToClone, usePropertyBasedClone); }
}
/// <summary> /// <summary>
/// Creates a shallow clone of the specified object, reusing any referenced objects /// Creates a deep clone of the specified object, also creating clones of all
/// </summary> /// child objects being referenced
/// <typeparam name="TCloned">Type of the object that will be cloned</typeparam> /// </summary>
/// <param name="objectToClone">Object that will be cloned</param> /// <typeparam name="TCloned">Type of the object that will be cloned</typeparam>
/// <param name="usePropertyBasedClone"> /// <param name="objectToClone">Object that will be cloned</param>
/// Whether to clone the object based on its properties only /// <param name="usePropertyBasedClone">
/// </param> /// Whether to clone the object based on its properties only
/// <returns>A shallow clone of the provided object</returns> /// </param>
TCloned ICloneFactory.ShallowClone<TCloned>( /// <returns>A deep clone of the provided object</returns>
TCloned objectToClone, bool usePropertyBasedClone TCloned ICloneFactory.DeepClone<TCloned>(
) { TCloned objectToClone, bool usePropertyBasedClone
return ExpressionTreeCloner.ShallowClone<TCloned>(objectToClone, usePropertyBasedClone); ) {
} return ExpressionTreeCloner.DeepClone<TCloned>(objectToClone, usePropertyBasedClone);
}
/// <summary> /// <summary>
/// Retrieves the existing clone method for the specified type or compiles one if /// Creates a shallow clone of the specified object, reusing any referenced objects
/// none exists for the type yet /// </summary>
/// </summary> /// <typeparam name="TCloned">Type of the object that will be cloned</typeparam>
/// <typeparam name="TCloned">Type for which a clone method will be retrieved</typeparam> /// <param name="objectToClone">Object that will be cloned</param>
/// <returns>The clone method for the specified type</returns> /// <param name="usePropertyBasedClone">
private static Func<TCloned, TCloned> getOrCreateDeepFieldBasedCloner<TCloned>() { /// Whether to clone the object based on its properties only
Type clonedType = typeof(TCloned); /// </param>
Delegate clonerAsDelegate; /// <returns>A shallow clone of the provided object</returns>
if(deepCloners.TryGetValue(clonedType, out clonerAsDelegate)) { TCloned ICloneFactory.ShallowClone<TCloned>(
return (Func<TCloned, TCloned>)clonerAsDelegate; TCloned objectToClone, bool usePropertyBasedClone
} else { ) {
Func<TCloned, TCloned> cloner = createDeepFieldBasedCloner<TCloned>(); return ExpressionTreeCloner.ShallowClone<TCloned>(objectToClone, usePropertyBasedClone);
deepCloners.TryAdd(clonedType, cloner); }
return cloner;
}
}
/// <summary> /// <summary>
/// Generates state transfer expressions to copy an array of primitive types /// Retrieves the existing clone method for the specified type or compiles one if
/// </summary> /// none exists for the type yet
/// <param name="clonedType">Type of array that will be cloned</param> /// </summary>
/// <param name="original">Variable expression for the original array</param> /// <param name="clonedType">Type for which a clone method will be retrieved</param>
/// <param name="variables">Receives variables used by the transfer expressions</param> /// <returns>The clone method for the specified type</returns>
/// <param name="transferExpressions">Receives the generated transfer expressions</param> private static Func<object, object> getOrCreateDeepFieldBasedCloner(Type clonedType) {
private static void generatePrimitiveArrayTransferExpressions( Func<object, object> cloner;
Type clonedType,
ParameterExpression original,
ICollection<ParameterExpression> variables,
ICollection<Expression> transferExpressions
) {
// We need a temporary variable because the IfThen expression is not suitable
// for returning values
ParameterExpression clone = Expression.Variable(clonedType, "clone");
variables.Add(clone);
// If the array referenced by 'original' is not null, call Array.Clone() on it if(!deepCloners.TryGetValue(clonedType, out cloner)) {
// and assign the result to our temporary variable cloner = createDeepFieldBasedCloner(clonedType);
MethodInfo arrayCloneMethodInfo = typeof(Array).GetMethod("Clone"); deepCloners.TryAdd(clonedType, cloner);
transferExpressions.Add( }
Expression.IfThen(
Expression.NotEqual(original, Expression.Constant(null)),
Expression.Assign(
clone,
Expression.Convert(
Expression.Call(original, arrayCloneMethodInfo),
clonedType
)
)
)
);
// Set the return value to the temporary variable return cloner;
transferExpressions.Add(clone); }
}
/// <summary> /// <summary>
/// Generates state transfer expressions to copy an array of complex types /// Generates state transfer expressions to copy an array of primitive types
/// </summary> /// </summary>
/// <param name="clonedType">Type of array that will be cloned</param> /// <param name="clonedType">Type of array that will be cloned</param>
/// <param name="original">Variable expression for the original array</param> /// <param name="original">Variable expression for the original array</param>
/// <param name="variables">Receives variables used by the transfer expressions</param> /// <param name="variables">Receives variables used by the transfer expressions</param>
/// <param name="transferExpressions">Receives the generated transfer expressions</param> /// <param name="transferExpressions">Receives the generated transfer expressions</param>
private static void generateComplexArrayTransferExpressions( /// <returns>The variable holding the cloned array</returns>
Type clonedType, private static ParameterExpression generatePrimitiveArrayTransferExpressions(
ParameterExpression original, Type clonedType,
IList<ParameterExpression> variables, Expression original,
ICollection<Expression> transferExpressions ICollection<ParameterExpression> variables,
) { ICollection<Expression> transferExpressions
// We need a temporary variable because the IfThen expression is not suitable ) {
// for returning values // We need a temporary variable because the IfThen expression is not suitable
ParameterExpression clone = Expression.Variable(clonedType, "clone"); // for returning values
variables.Add(clone); ParameterExpression clone = Expression.Variable(typeof(object));
variables.Add(clone);
int dimensionCount = clonedType.GetArrayRank(); // If the array referenced by 'original' is not null, call Array.Clone() on it
int baseVariableIndex = variables.Count; // and assign the result to our temporary variable
var arrayTransferExpressions = new List<Expression>(); MethodInfo arrayCloneMethodInfo = typeof(Array).GetMethod("Clone");
Type elementType = clonedType.GetElementType(); transferExpressions.Add(
Expression.IfThen(
Expression.NotEqual(original, Expression.Constant(null)),
Expression.Assign(
clone,
Expression.Convert(
Expression.Call(
Expression.Convert(original, typeof(Array)), arrayCloneMethodInfo
),
clonedType
)
)
)
);
// Retrieve the length of each of the array's dimensions return clone;
MethodInfo arrayGetLengthMethodInfo = typeof(Array).GetMethod("GetLength"); }
for(int index = 0; index < dimensionCount; ++index) {
ParameterExpression length = Expression.Variable(typeof(int));
variables.Add(length);
arrayTransferExpressions.Add(
Expression.Assign(
length,
Expression.Call(original, arrayGetLengthMethodInfo, Expression.Constant(index))
)
);
}
// Create a new array of identical size /// <summary>
switch(dimensionCount) { /// Generates state transfer expressions to copy an array of complex types
case 1: { /// </summary>
MethodInfo arrayCreateInstanceMethodInfo = typeof(Array).GetMethod( /// <param name="clonedType">Type of array that will be cloned</param>
"CreateInstance", new Type[] { typeof(Type), typeof(int) } /// <param name="original">Variable expression for the original array</param>
); /// <param name="variables">Receives variables used by the transfer expressions</param>
arrayTransferExpressions.Add( /// <param name="transferExpressions">Receives the generated transfer expressions</param>
Expression.Assign( /// <returns>The variable holding the cloned array</returns>
clone, private static ParameterExpression generateComplexArrayTransferExpressions(
Expression.Convert( Type clonedType,
Expression.Call( Expression original,
arrayCreateInstanceMethodInfo, IList<ParameterExpression> variables,
Expression.Constant(elementType), ICollection<Expression> transferExpressions
variables[baseVariableIndex] ) {
), // We need a temporary variable because the IfThen expression is not suitable
clonedType // for returning values
) ParameterExpression clone = Expression.Variable(clonedType);
) variables.Add(clone);
); ParameterExpression typedOriginal = Expression.Variable(clonedType);
break; variables.Add(typedOriginal);
}
case 2: {
MethodInfo arrayCreateInstanceMethodInfo = typeof(Array).GetMethod(
"CreateInstance", new Type[] { typeof(Type), typeof(int), typeof(int) }
);
arrayTransferExpressions.Add(
Expression.Assign(
clone,
Expression.Convert(
Expression.Call(
arrayCreateInstanceMethodInfo,
Expression.Constant(elementType),
variables[baseVariableIndex],
variables[baseVariableIndex + 1]
),
clonedType
)
)
);
break;
}
case 3: {
MethodInfo arrayCreateInstanceMethodInfo = typeof(Array).GetMethod(
"CreateInstance", new Type[] { typeof(Type), typeof(int), typeof(int), typeof(int) }
);
arrayTransferExpressions.Add(
Expression.Assign(
clone,
Expression.Convert(
Expression.Call(
arrayCreateInstanceMethodInfo,
Expression.Constant(elementType),
variables[baseVariableIndex],
variables[baseVariableIndex + 1],
variables[baseVariableIndex + 2]
),
clonedType
)
)
);
break;
}
default: {
throw new InvalidOperationException("Unsupported array dimension count");
}
} transferExpressions.Add(
Expression.Assign(typedOriginal, Expression.Convert(original, clonedType))
);
// Only execute the array transfer expressions if the array is not null int dimensionCount = clonedType.GetArrayRank();
transferExpressions.Add( int baseVariableIndex = variables.Count;
Expression.IfThen( var arrayTransferExpressions = new List<Expression>();
Expression.NotEqual(original, Expression.Constant(null)), Type elementType = clonedType.GetElementType();
Expression.Block(arrayTransferExpressions)
)
);
// Set the return value to the temporary variable // Retrieve the length of each of the array's dimensions
transferExpressions.Add(clone); MethodInfo arrayGetLengthMethodInfo = typeof(Array).GetMethod("GetLength");
} for(int index = 0; index < dimensionCount; ++index) {
ParameterExpression length = Expression.Variable(typeof(int));
variables.Add(length);
arrayTransferExpressions.Add(
Expression.Assign(
length,
Expression.Call(
typedOriginal, arrayGetLengthMethodInfo, Expression.Constant(index)
)
)
);
}
/// <summary>Generates state transfer expressions to copy a complex type</summary> // Create a new array of identical size
/// <param name="clonedType">Complex type that will be cloned</param> switch(dimensionCount) {
/// <param name="original">Variable expression for the original instance</param> case 1: {
/// <param name="variables">Receives variables used by the transfer expressions</param> MethodInfo arrayCreateInstanceMethodInfo = typeof(Array).GetMethod(
/// <param name="transferExpressions">Receives the generated transfer expressions</param> "CreateInstance", new Type[] { typeof(Type), typeof(int) }
private static void generateComplexTypeTransferExpressions( );
Type clonedType, arrayTransferExpressions.Add(
ParameterExpression original, Expression.Assign(
ICollection<ParameterExpression> variables, clone,
ICollection<Expression> transferExpressions Expression.Convert(
) { Expression.Call(
// We need a temporary variable because the IfThen expression is not suitable arrayCreateInstanceMethodInfo,
// for returning values Expression.Constant(elementType),
ParameterExpression clone = Expression.Variable(clonedType, "clone"); variables[baseVariableIndex]
variables.Add(clone); ),
clonedType
)
)
);
break;
}
case 2: {
MethodInfo arrayCreateInstanceMethodInfo = typeof(Array).GetMethod(
"CreateInstance", new Type[] { typeof(Type), typeof(int), typeof(int) }
);
arrayTransferExpressions.Add(
Expression.Assign(
clone,
Expression.Convert(
Expression.Call(
arrayCreateInstanceMethodInfo,
Expression.Constant(elementType),
variables[baseVariableIndex],
variables[baseVariableIndex + 1]
),
clonedType
)
)
);
break;
}
case 3: {
MethodInfo arrayCreateInstanceMethodInfo = typeof(Array).GetMethod(
"CreateInstance", new Type[] { typeof(Type), typeof(int), typeof(int), typeof(int) }
);
arrayTransferExpressions.Add(
Expression.Assign(
clone,
Expression.Convert(
Expression.Call(
arrayCreateInstanceMethodInfo,
Expression.Constant(elementType),
variables[baseVariableIndex],
variables[baseVariableIndex + 1],
variables[baseVariableIndex + 2]
),
clonedType
)
)
);
break;
}
default: {
throw new InvalidOperationException("Unsupported array dimension count");
}
}
var complexTransferExpressions = new List<Expression>();
complexTransferExpressions.Add(Expression.Assign(clone, Expression.New(clonedType)));
FieldInfo[] fieldInfos = clonedType.GetFields( // Only execute the array transfer expressions if the array is not null
BindingFlags.Public | BindingFlags.NonPublic | transferExpressions.Add(
BindingFlags.Instance | BindingFlags.FlattenHierarchy Expression.IfThen(
); Expression.NotEqual(original, Expression.Constant(null)),
for(int index = 0; index < fieldInfos.Length; ++index) { Expression.Block(arrayTransferExpressions)
FieldInfo fieldInfo = fieldInfos[index]; )
Type fieldType = fieldInfo.FieldType; );
if(fieldType.IsPrimitive) { return clone;
complexTransferExpressions.Add( }
Expression.Assign(
Expression.Field(clone, fieldInfo),
Expression.Field(original, fieldInfo)
)
);
}
}
transferExpressions.Add( /// <summary>Generates state transfer expressions to copy a complex type</summary>
Expression.IfThen( /// <param name="clonedType">Complex type that will be cloned</param>
Expression.NotEqual(original, Expression.Constant(null)), /// <param name="original">Variable expression for the original instance</param>
Expression.Block(complexTransferExpressions) /// <param name="variables">Receives variables used by the transfer expressions</param>
) /// <param name="transferExpressions">Receives the generated transfer expressions</param>
); /// <returns>The variable holding the cloned array</returns>
private static ParameterExpression generateComplexTypeTransferExpressions(
Type clonedType,
Expression original,
IList<ParameterExpression> variables,
ICollection<Expression> transferExpressions
) {
// Create a variable to hold the clone and begin by assigning a new instance of
// the cloned type to it.
ParameterExpression clone = Expression.Variable(clonedType);
variables.Add(clone);
transferExpressions.Add(Expression.Assign(clone, Expression.New(clonedType)));
// Set the return value to the temporary variable // To access the fields of the original type, we need it to be of the actual
transferExpressions.Add(clone); // type instead of an object, so perform a downcast
} ParameterExpression typedOriginal = Expression.Variable(clonedType);
variables.Add(typedOriginal);
transferExpressions.Add(
Expression.Assign(typedOriginal, Expression.Convert(original, clonedType))
);
/// <summary>Compiles a method that creates a clone of an object</summary> // Now enumerate all of the type's fields and generate transfer expressions for
/// <typeparam name="TCloned">Type for which a clone method will be created</typeparam> // each of them
/// <returns>A method that clones an object of the provided type</returns> FieldInfo[] fieldInfos = clonedType.GetFields(
private static Func<TCloned, TCloned> createDeepFieldBasedCloner<TCloned>() { BindingFlags.Public | BindingFlags.NonPublic |
Type clonedType = typeof(TCloned); BindingFlags.Instance | BindingFlags.FlattenHierarchy
);
for(int index = 0; index < fieldInfos.Length; ++index) {
FieldInfo fieldInfo = fieldInfos[index];
Type fieldType = fieldInfo.FieldType;
ParameterExpression original = Expression.Parameter(typeof(TCloned), "original"); if(fieldType.IsPrimitive || (fieldType == typeof(string))) {
ParameterExpression clone = Expression.Variable(typeof(TCloned), "clone"); // Primitive types and strings can be transferred by simple assignment
transferExpressions.Add(
Expression.Assign(
Expression.Field(clone, fieldInfo),
Expression.Field(typedOriginal, fieldInfo)
)
);
} else if(fieldType.IsValueType) {
// TODO: Copy field without null check
} else {
var fieldTransferExpressions = new List<Expression>();
var fieldVariables = new List<ParameterExpression>();
var transferExpressions = new List<Expression>(); Expression fieldClone;
var variables = new List<ParameterExpression>(); if(fieldType.IsArray) {
/*
Type elementType = fieldType.GetElementType();
if(elementType.IsPrimitive || (elementType == typeof(string))) {
fieldClone = generatePrimitiveArrayTransferExpressions(
fieldType,
Expression.Field(typedOriginal, fieldInfo),
fieldVariables,
fieldTransferExpressions
);
} else {
fieldClone = generateComplexArrayTransferExpressions(
fieldType,
Expression.Field(typedOriginal, fieldInfo),
fieldVariables,
fieldTransferExpressions
);
}
*/
fieldClone = Expression.Field(typedOriginal, fieldInfo);
fieldTransferExpressions.Add(fieldClone);
} else {
MethodInfo getOrCreateClonerMethodInfo = typeof(ExpressionTreeCloner).GetMethod(
"getOrCreateDeepFieldBasedCloner",
BindingFlags.NonPublic | BindingFlags.Static
);
MethodInfo getTypeMethodInfo = typeof(object).GetMethod("GetType");
MethodInfo invokeMethodInfo = typeof(Func<object, object>).GetMethod("Invoke");
if(clonedType.IsPrimitive || (clonedType == typeof(string))) { fieldTransferExpressions.Add(
transferExpressions.Add(original); // primitives are copied on assignment Expression.Assign(
} else if(clonedType.IsArray) { Expression.Field(clone, fieldInfo),
Type elementType = clonedType.GetElementType(); Expression.Convert(
if(elementType.IsPrimitive || (elementType == typeof(string))) { Expression.Call(
generatePrimitiveArrayTransferExpressions( Expression.Call(
clonedType, original, variables, transferExpressions getOrCreateClonerMethodInfo,
); Expression.Call(
} else { Expression.Field(typedOriginal, fieldInfo), getTypeMethodInfo
generateComplexArrayTransferExpressions( )
clonedType, original, variables, transferExpressions ),
); invokeMethodInfo,
} Expression.Field(typedOriginal, fieldInfo)
} else { ),
generateComplexTypeTransferExpressions( fieldType
clonedType, original, variables, transferExpressions )
); )
} );
}
Expression<Func<TCloned, TCloned>> expression; transferExpressions.Add(
if(variables.Count > 0) { Expression.IfThen(
expression = Expression.Lambda<Func<TCloned, TCloned>>( Expression.NotEqual(
Expression.Block(variables, transferExpressions), original Expression.Field(typedOriginal, fieldInfo), Expression.Constant(null)
); ),
} else if(transferExpressions.Count == 1) { Expression.Block(fieldVariables, fieldTransferExpressions)
expression = Expression.Lambda<Func<TCloned, TCloned>>( )
transferExpressions[0], original );
);
} else {
expression = Expression.Lambda<Func<TCloned, TCloned>>(
Expression.Block(transferExpressions), original
);
}
return expression.Compile(); }
} }
return clone;
}
/// <summary>Compiles a method that creates a clone of an object</summary>
/// <param name="clonedType">Type for which a clone method will be created</param>
/// <returns>A method that clones an object of the provided type</returns>
private static Func<object, object> createDeepFieldBasedCloner(Type clonedType) {
ParameterExpression original = Expression.Parameter(typeof(object), "original");
var transferExpressions = new List<Expression>();
var variables = new List<ParameterExpression>();
if(clonedType.IsPrimitive || (clonedType == typeof(string))) {
// Primitives and strings are copied on direct assignment
transferExpressions.Add(original);
} else if(clonedType.IsArray) {
ParameterExpression clone;
Type elementType = clonedType.GetElementType();
if(elementType.IsPrimitive || (elementType == typeof(string))) {
clone = generatePrimitiveArrayTransferExpressions(
clonedType, original, variables, transferExpressions
);
} else {
clone = generateComplexArrayTransferExpressions(
clonedType, original, variables, transferExpressions
);
}
transferExpressions.Add(clone);
//clone = original;
} else {
transferExpressions.Add(
generateComplexTypeTransferExpressions(
clonedType, original, variables, transferExpressions
)
);
}
Expression<Func<object, object>> expression;
if(variables.Count > 0) {
expression = Expression.Lambda<Func<object, object>>(
Expression.Block(variables, transferExpressions), original
);
} else if(transferExpressions.Count == 1) {
expression = Expression.Lambda<Func<object, object>>(
transferExpressions[0], original
);
} else {
expression = Expression.Lambda<Func<object, object>>(
Expression.Block(transferExpressions), original
);
}
return expression.Compile();
}
#if false #if false
/// <summary> /// <summary>
@ -463,12 +549,12 @@ namespace Nuclex.Support.Cloning {
} }
#endif #endif
/// <summary>Compiled cloners that perform shallow clone operations</summary> /// <summary>Compiled cloners that perform shallow clone operations</summary>
private static ConcurrentDictionary<Type, Delegate> shallowCloners; private static ConcurrentDictionary<Type, Func<object, object>> shallowCloners;
/// <summary>Compiled cloners that perform deep clone operations</summary> /// <summary>Compiled cloners that perform deep clone operations</summary>
private static ConcurrentDictionary<Type, Delegate> deepCloners; private static ConcurrentDictionary<Type, Func<object, object>> deepCloners;
} }
} // namespace Nuclex.Support.Cloning } // namespace Nuclex.Support.Cloning