Property-based shallow clones of value types are now also working correctly in the expression tree cloner

git-svn-id: file:///srv/devel/repo-conversion/nusu@247 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2012-02-08 18:47:45 +00:00
parent d091061baa
commit 48e1674912

View File

@ -122,88 +122,138 @@ namespace Nuclex.Support.Cloning {
return Expression.Lambda<Func<object, object>>(resultExpression, original).Compile(); return Expression.Lambda<Func<object, object>>(resultExpression, original).Compile();
} }
/// <summary>Compiles a method that creates a deep clone of an object</summary> /// <summary>Compiles a method that creates a deep clone of an object</summary>
/// <param name="clonedType">Type for which a clone method will be created</param> /// <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> /// <returns>A method that clones an object of the provided type</returns>
/// <remarks> /// <remarks>
/// <para> /// <para>
/// The 'null' check is supposed to take place before running the cloner. This /// The 'null' check is supposed to take place before running the cloner. This
/// avoids having redundant 'null' checks on nested types - first before calling /// avoids having redundant 'null' checks on nested types - first before calling
/// GetType() on the property to be cloned and second when runner the matching /// GetType() on the property to be cloned and second when runner the matching
/// cloner for the property. /// cloner for the property.
/// </para> /// </para>
/// <para> /// <para>
/// This design also enables the cloning of nested value types (which can never /// This design also enables the cloning of nested value types (which can never
/// be null) without any null check whatsoever. /// be null) without any null check whatsoever.
/// </para> /// </para>
/// </remarks> /// </remarks>
private static Func<object, object> createShallowPropertyBasedCloner(Type clonedType) { private static Func<object, object> createShallowPropertyBasedCloner(Type clonedType) {
ParameterExpression original = Expression.Parameter(typeof(object), "original"); ParameterExpression original = Expression.Parameter(typeof(object), "original");
var transferExpressions = new List<Expression>(); var transferExpressions = new List<Expression>();
var variables = new List<ParameterExpression>(); var variables = new List<ParameterExpression>();
if(clonedType.IsPrimitive || (clonedType == typeof(string))) { if(clonedType.IsPrimitive || (clonedType == typeof(string))) {
// Primitives and strings are copied on direct assignment // Primitives and strings are copied on direct assignment
transferExpressions.Add(original); transferExpressions.Add(original);
} else if(clonedType.IsArray) { } else if(clonedType.IsArray) {
transferExpressions.Add( transferExpressions.Add(
generatePropertyBasedPrimitiveArrayTransferExpressions( generatePropertyBasedPrimitiveArrayTransferExpressions(
clonedType, original, variables, transferExpressions clonedType, original, variables, transferExpressions
) )
); );
} else { } else {
// We need a variable to hold the clone because due to the assignments it // We need a variable to hold the clone because due to the assignments it
// won't be last in the block when we're finished // won't be last in the block when we're finished
ParameterExpression clone = Expression.Variable(clonedType); ParameterExpression clone = Expression.Variable(clonedType);
variables.Add(clone); variables.Add(clone);
transferExpressions.Add(Expression.Assign(clone, Expression.New(clonedType)));
// Give it a new instance of the type being cloned // To access the properties of the original type, we need it to be of the actual
transferExpressions.Add(Expression.Assign(clone, Expression.New(clonedType))); // 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))
);
// To access the properties of the original type, we need it to be of the actual generateShallowPropertyBasedComplexCloneExpressions(
// type instead of an object, so perform a downcast clonedType, typedOriginal, clone, transferExpressions, variables
ParameterExpression typedOriginal = Expression.Variable(clonedType); );
variables.Add(typedOriginal);
transferExpressions.Add(
Expression.Assign(typedOriginal, Expression.Convert(original, clonedType))
);
// Enumerate all of the type's properties and generate transfer expressions for each // Make sure the clone is the last thing in the block to set the return value
PropertyInfo[] propertyInfos = clonedType.GetProperties( transferExpressions.Add(clone);
BindingFlags.Public | BindingFlags.NonPublic | }
BindingFlags.Instance | BindingFlags.FlattenHierarchy
);
for(int index = 0; index < propertyInfos.Length; ++index) {
PropertyInfo propertyInfo = propertyInfos[index];
transferExpressions.Add( // Turn all transfer expressions into a single block if necessary
Expression.Assign( Expression resultExpression;
Expression.Property(clone, propertyInfo), if((transferExpressions.Count == 1) && (variables.Count == 0)) {
Expression.Property(typedOriginal, propertyInfo) resultExpression = transferExpressions[0];
) } else {
); resultExpression = Expression.Block(variables, transferExpressions);
} }
// Make sure the clone is the last thing in the block to set the return value // Value types require manual boxing
transferExpressions.Add(clone); if(clonedType.IsValueType) {
} resultExpression = Expression.Convert(resultExpression, typeof(object));
}
// Turn all transfer expressions into a single block if necessary return Expression.Lambda<Func<object, object>>(resultExpression, original).Compile();
Expression resultExpression; }
if((transferExpressions.Count == 1) && (variables.Count == 0)) {
resultExpression = transferExpressions[0];
} else {
resultExpression = Expression.Block(variables, transferExpressions);
}
// Value types require manual boxing /// <summary>
if(clonedType.IsValueType) { /// Generates expressions to transfer the properties of a complex value type
resultExpression = Expression.Convert(resultExpression, typeof(object)); /// </summary>
} /// <param name="clonedType">Complex value type that will be cloned</param>
/// <param name="original">Original instance whose properties will be cloned</param>
/// <param name="clone">Target instance into which the properties will be copied</param>
/// <param name="transferExpressions">Receives the value transfer expressions</param>
/// <param name="variables">Receives temporary variables used during the clone</param>
private static void generateShallowPropertyBasedComplexCloneExpressions(
Type clonedType,
ParameterExpression original,
ParameterExpression clone,
ICollection<Expression> transferExpressions,
ICollection<ParameterExpression> variables
) {
// Enumerate all of the type's properties and generate transfer expressions for each
PropertyInfo[] propertyInfos = clonedType.GetProperties(
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.FlattenHierarchy
);
for(int index = 0; index < propertyInfos.Length; ++index) {
PropertyInfo propertyInfo = propertyInfos[index];
Type propertyType = propertyInfo.PropertyType;
return Expression.Lambda<Func<object, object>>(resultExpression, original).Compile(); if(propertyType.IsPrimitive || (propertyType == typeof(string))) {
} transferExpressions.Add(
Expression.Assign(
Expression.Property(clone, propertyInfo),
Expression.Property(original, propertyInfo)
)
);
} else if(propertyType.IsValueType) {
ParameterExpression originalProperty = Expression.Variable(propertyType);
variables.Add(originalProperty);
transferExpressions.Add(
Expression.Assign(
originalProperty, Expression.Property(original, propertyInfo)
)
);
ParameterExpression clonedProperty = Expression.Variable(propertyType);
variables.Add(clonedProperty);
transferExpressions.Add(
Expression.Assign(clonedProperty, Expression.New(propertyType))
);
generateShallowPropertyBasedComplexCloneExpressions(propertyType, originalProperty, clonedProperty, transferExpressions, variables);
transferExpressions.Add(
Expression.Assign(
Expression.Property(clone, propertyInfo), clonedProperty
)
);
} else {
transferExpressions.Add(
Expression.Assign(
Expression.Property(clone, propertyInfo),
Expression.Property(original, propertyInfo)
)
);
}
}
}
/// <summary> /// <summary>
/// Generates state transfer expressions to copy an array of primitive types /// Generates state transfer expressions to copy an array of primitive types