ExpressionTreeCloner now can create shallow field-based clones of objects without a default constructor (before this, only deep clones worked); search-and-replace fix - some comments referred to something called 'propertys'; all cloners now correctly handle being passed null values (a clone of 'null' is also 'null' per definition)

git-svn-id: file:///srv/devel/repo-conversion/nusu@246 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2012-02-08 17:38:20 +00:00
parent 15300676ba
commit d091061baa
8 changed files with 167 additions and 22 deletions

View File

@ -169,7 +169,20 @@ namespace Nuclex.Support.Cloning {
);
// Give it a new instance of the type being cloned
transferExpressions.Add(Expression.Assign(clone, Expression.New(clonedType)));
MethodInfo getUninitializedObjectMethodInfo = typeof(FormatterServices).GetMethod(
"GetUninitializedObject", BindingFlags.Static | BindingFlags.Public
);
transferExpressions.Add(
Expression.Assign(
clone,
Expression.Convert(
Expression.Call(
getUninitializedObjectMethodInfo, Expression.Constant(clonedType)
),
clonedType
)
)
);
// Enumerate all of the type's fields and generate transfer expressions for each
FieldInfo[] fieldInfos = clonedType.GetFields(

View File

@ -65,7 +65,7 @@ namespace Nuclex.Support.Cloning {
)
);
} else {
// To access the propertys of the original type, we need it to be of the actual
// To access the properties of the original type, we need it to be of the actual
// type instead of an object, so perform a downcast
ParameterExpression typedOriginal = Expression.Variable(clonedType);
variables.Add(typedOriginal);
@ -89,7 +89,7 @@ namespace Nuclex.Support.Cloning {
// Give it a new instance of the type being cloned
transferExpressions.Add(Expression.Assign(clone, Expression.New(clonedType)));
// To access the propertys of the original type, we need it to be of the actual
// To access the properties of the original type, we need it to be of the actual
// type instead of an object, so perform a downcast
ParameterExpression typedOriginal = Expression.Variable(clonedType);
variables.Add(typedOriginal);
@ -122,6 +122,89 @@ namespace Nuclex.Support.Cloning {
return Expression.Lambda<Func<object, object>>(resultExpression, original).Compile();
}
/// <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>
/// <returns>A method that clones an object of the provided type</returns>
/// <remarks>
/// <para>
/// The 'null' check is supposed to take place before running the cloner. This
/// avoids having redundant 'null' checks on nested types - first before calling
/// GetType() on the property to be cloned and second when runner the matching
/// cloner for the property.
/// </para>
/// <para>
/// This design also enables the cloning of nested value types (which can never
/// be null) without any null check whatsoever.
/// </para>
/// </remarks>
private static Func<object, object> createShallowPropertyBasedCloner(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) {
transferExpressions.Add(
generatePropertyBasedPrimitiveArrayTransferExpressions(
clonedType, original, variables, transferExpressions
)
);
} else {
// 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
ParameterExpression clone = Expression.Variable(clonedType);
variables.Add(clone);
// Give it a new instance of the type being cloned
transferExpressions.Add(Expression.Assign(clone, Expression.New(clonedType)));
// To access the properties of the original type, we need it to be of the actual
// 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))
);
// 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];
transferExpressions.Add(
Expression.Assign(
Expression.Property(clone, propertyInfo),
Expression.Property(typedOriginal, propertyInfo)
)
);
}
// Make sure the clone is the last thing in the block to set the return value
transferExpressions.Add(clone);
}
// Turn all transfer expressions into a single block if necessary
Expression resultExpression;
if((transferExpressions.Count == 1) && (variables.Count == 0)) {
resultExpression = transferExpressions[0];
} else {
resultExpression = Expression.Block(variables, transferExpressions);
}
// Value types require manual boxing
if(clonedType.IsValueType) {
resultExpression = Expression.Convert(resultExpression, typeof(object));
}
return Expression.Lambda<Func<object, object>>(resultExpression, original).Compile();
}
/// <summary>
/// Generates state transfer expressions to copy an array of primitive types
/// </summary>
@ -236,7 +319,7 @@ namespace Nuclex.Support.Cloning {
)
);
} else if(elementType.IsValueType) {
// Arrays of complex value types can be transferred by assigning all propertys
// Arrays of complex value types can be transferred by assigning all properties
// of the source array element to the destination array element (cloning
// any nested reference types appropriately)
generatePropertyBasedComplexTypeTransferExpressions(
@ -249,7 +332,7 @@ namespace Nuclex.Support.Cloning {
} else {
// Arrays of reference types need to be cloned by creating a new instance
// of the reference type and then transferring the propertys over
// of the reference type and then transferring the properties over
ParameterExpression originalElement = Expression.Variable(elementType);
loopVariables.Add(originalElement);
@ -279,7 +362,7 @@ namespace Nuclex.Support.Cloning {
Expression.Assign(Expression.ArrayAccess(clone, indexes), clonedElement)
);
} else {
// Complex types are cloned by checking their actual, concrete type (propertys
// Complex types are cloned by checking their actual, concrete type (properties
// may be typed to an interface or base class) and requesting a cloner for that
// type during runtime
MethodInfo getOrCreateClonerMethodInfo = typeof(ExpressionTreeCloner).GetMethod(
@ -363,7 +446,7 @@ namespace Nuclex.Support.Cloning {
IList<ParameterExpression> variables,
ICollection<Expression> transferExpressions
) {
// Enumerate all of the type's propertys and generate transfer expressions for each
// 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
@ -395,7 +478,7 @@ namespace Nuclex.Support.Cloning {
Expression.Assign(clonedProperty, Expression.New(propertyType))
);
// A nested value type is part of the parent and will have its propertys directly
// A nested value type is part of the parent and will have its properties directly
// assigned without boxing, new instance creation or anything like that.
generatePropertyBasedComplexTypeTransferExpressions(
propertyType,
@ -428,6 +511,7 @@ namespace Nuclex.Support.Cloning {
/// <param name="transferExpressions">
/// Receives the expression generated to transfer the values
/// </param>
/// <param name="variables">Receives variables used by the transfer expressions</param>
/// <param name="propertyInfo">Reflection informations about the property being cloned</param>
/// <param name="propertyType">Type of the property being cloned</param>
private static void generatePropertyBasedReferenceTypeTransferExpressions(
@ -480,7 +564,7 @@ namespace Nuclex.Support.Cloning {
Expression.Assign(Expression.Property(clone, propertyInfo), propertyClone)
);
} else {
// Complex types are cloned by checking their actual, concrete type (propertys
// Complex types are cloned by checking their actual, concrete type (properties
// may be typed to an interface or base class) and requesting a cloner for that
// type during runtime
MethodInfo getOrCreateClonerMethodInfo = typeof(ExpressionTreeCloner).GetMethod(

View File

@ -36,6 +36,15 @@ namespace Nuclex.Support.Cloning {
this.cloneFactory = new ExpressionTreeCloner();
}
/// <summary>Verifies that cloning a null object simply returns null</summary>
[Test]
public void CloningNullYieldsNull() {
Assert.IsNull(this.cloneFactory.DeepFieldClone<object>(null));
Assert.IsNull(this.cloneFactory.DeepPropertyClone<object>(null));
Assert.IsNull(this.cloneFactory.ShallowFieldClone<object>(null));
Assert.IsNull(this.cloneFactory.ShallowPropertyClone<object>(null));
}
/// <summary>Verifies that clones of primitive types can be created</summary>
[Test]
public void PrimitiveTypesCanBeCloned() {
@ -150,14 +159,13 @@ namespace Nuclex.Support.Cloning {
VerifyClone(original, clone, isDeepClone: true, isPropertyBasedClone: false);
}
#if false
/// <summary>
/// Verifies that a property-based shallow clone of a value type can be performed
/// </summary>
[Test]
public void ShallowPropertyBasedClonesOfValueTypesCanBeMade() {
HierarchicalValueType original = CreateValueType();
HierarchicalValueType clone = this.cloneFactory.ShallowClone(original, true);
HierarchicalValueType clone = this.cloneFactory.ShallowPropertyClone(original);
VerifyClone(ref original, ref clone, isDeepClone: false, isPropertyBasedClone: true);
}
@ -167,10 +175,9 @@ namespace Nuclex.Support.Cloning {
[Test]
public void ShallowPropertyBasedClonesOfReferenceTypesCanBeMade() {
HierarchicalReferenceType original = CreateReferenceType();
HierarchicalReferenceType clone = this.cloneFactory.ShallowClone(original, true);
HierarchicalReferenceType clone = this.cloneFactory.ShallowPropertyClone(original);
VerifyClone(original, clone, isDeepClone: false, isPropertyBasedClone: true);
}
#endif
/// <summary>
/// Verifies that a property-based deep clone of a value type can be performed

View File

@ -101,8 +101,7 @@ namespace Nuclex.Support.Cloning {
return default(TCloned);
}
throw new NotImplementedException("Not implemented yet");
Func<object, object> cloner = getOrCreateShallowFieldBasedCloner(typeof(TCloned));
Func<object, object> cloner = getOrCreateShallowPropertyBasedCloner(typeof(TCloned));
return (TCloned)cloner(objectToCloneAsObject);
}
@ -239,8 +238,7 @@ namespace Nuclex.Support.Cloning {
Func<object, object> cloner;
if(!shallowPropertyBasedCloners.TryGetValue(clonedType, out cloner)) {
throw new NotImplementedException();
//cloner = createShallowPropertyBasedCloner(clonedType);
cloner = createShallowPropertyBasedCloner(clonedType);
shallowPropertyBasedCloners.TryAdd(clonedType, cloner);
}

View File

@ -36,6 +36,15 @@ namespace Nuclex.Support.Cloning {
this.cloneFactory = new ReflectionCloner();
}
/// <summary>Verifies that cloning a null object simply returns null</summary>
[Test]
public void CloningNullYieldsNull() {
Assert.IsNull(this.cloneFactory.DeepFieldClone<object>(null));
Assert.IsNull(this.cloneFactory.DeepPropertyClone<object>(null));
Assert.IsNull(this.cloneFactory.ShallowFieldClone<object>(null));
Assert.IsNull(this.cloneFactory.ShallowPropertyClone<object>(null));
}
/// <summary>Verifies that clones of primitive types can be created</summary>
[Test]
public void PrimitiveTypesCanBeCloned() {

View File

@ -58,9 +58,6 @@ namespace Nuclex.Support.Cloning {
/// </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 static TCloned ShallowPropertyClone<TCloned>(TCloned objectToClone) {
Type originalType = objectToClone.GetType();
@ -83,7 +80,12 @@ namespace Nuclex.Support.Cloning {
/// <param name="objectToClone">Object that will be cloned</param>
/// <returns>A deep clone of the provided object</returns>
public static TCloned DeepFieldClone<TCloned>(TCloned objectToClone) {
return (TCloned)deepCloneSingleFieldBased(objectToClone);
object objectToCloneAsObject = objectToClone;
if(objectToClone == null) {
return default(TCloned);
} else {
return (TCloned)deepCloneSingleFieldBased(objectToCloneAsObject);
}
}
/// <summary>
@ -94,7 +96,12 @@ namespace Nuclex.Support.Cloning {
/// <param name="objectToClone">Object that will be cloned</param>
/// <returns>A deep clone of the provided object</returns>
public static TCloned DeepPropertyClone<TCloned>(TCloned objectToClone) {
return (TCloned)deepCloneSinglePropertyBased(objectToClone);
object objectToCloneAsObject = objectToClone;
if(objectToClone == null) {
return default(TCloned);
} else {
return (TCloned)deepCloneSinglePropertyBased(objectToCloneAsObject);
}
}
/// <summary>
@ -104,6 +111,11 @@ namespace Nuclex.Support.Cloning {
/// <param name="objectToClone">Object that will be cloned</param>
/// <returns>A shallow clone of the provided object</returns>
TCloned ICloneFactory.ShallowFieldClone<TCloned>(TCloned objectToClone) {
if(typeof(TCloned).IsClass || typeof(TCloned).IsArray) {
if(ReferenceEquals(objectToClone, null)) {
return default(TCloned);
}
}
return ReflectionCloner.ShallowFieldClone<TCloned>(objectToClone);
}
@ -114,6 +126,11 @@ namespace Nuclex.Support.Cloning {
/// <param name="objectToClone">Object that will be cloned</param>
/// <returns>A shallow clone of the provided object</returns>
TCloned ICloneFactory.ShallowPropertyClone<TCloned>(TCloned objectToClone) {
if(typeof(TCloned).IsClass || typeof(TCloned).IsArray) {
if(ReferenceEquals(objectToClone, null)) {
return default(TCloned);
}
}
return ReflectionCloner.ShallowPropertyClone<TCloned>(objectToClone);
}

View File

@ -36,6 +36,13 @@ namespace Nuclex.Support.Cloning {
this.cloneFactory = new SerializationCloner();
}
/// <summary>Verifies that cloning a null object simply returns null</summary>
[Test]
public void CloningNullYieldsNull() {
Assert.IsNull(this.cloneFactory.DeepFieldClone<object>(null));
Assert.IsNull(this.cloneFactory.DeepPropertyClone<object>(null));
}
/// <summary>Verifies that clones of primitive types can be created</summary>
[Test]
public void PrimitiveTypesCanBeCloned() {

View File

@ -244,6 +244,11 @@ namespace Nuclex.Support.Cloning {
/// <param name="objectToClone">Object that will be cloned</param>
/// <returns>A deep clone of the provided object</returns>
public static TCloned DeepFieldClone<TCloned>(TCloned objectToClone) {
if(typeof(TCloned).IsClass || typeof(TCloned).IsArray) {
if(ReferenceEquals(objectToClone, null)) {
return default(TCloned);
}
}
using(var memoryStream = new MemoryStream()) {
fieldBasedFormatter.Serialize(memoryStream, objectToClone);
memoryStream.Position = 0;
@ -259,6 +264,11 @@ namespace Nuclex.Support.Cloning {
/// <param name="objectToClone">Object that will be cloned</param>
/// <returns>A deep clone of the provided object</returns>
public static TCloned DeepPropertyClone<TCloned>(TCloned objectToClone) {
if(typeof(TCloned).IsClass || typeof(TCloned).IsArray) {
if(ReferenceEquals(objectToClone, null)) {
return default(TCloned);
}
}
using(var memoryStream = new MemoryStream()) {
propertyBasedFormatter.Serialize(memoryStream, objectToClone);
memoryStream.Position = 0;