Fully implemented property-based cloning for the expression tree cloner

git-svn-id: file:///srv/devel/repo-conversion/nusu@244 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2012-02-08 16:51:58 +00:00
parent db8c93eabd
commit 0f2bb60ea5
4 changed files with 89 additions and 32 deletions

View File

@ -29,8 +29,6 @@ namespace Nuclex.Support.Cloning {
partial class ExpressionTreeCloner : ICloneFactory { partial class ExpressionTreeCloner : ICloneFactory {
#if false
/// <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>
@ -383,18 +381,40 @@ namespace Nuclex.Support.Cloning {
) )
); );
} else if(propertyType.IsValueType) { } else if(propertyType.IsValueType) {
ParameterExpression originalProperty = Expression.Variable(propertyType);
variables.Add(originalProperty);
ParameterExpression clonedProperty = Expression.Variable(propertyType);
variables.Add(clonedProperty);
transferExpressions.Add(
Expression.Assign(
originalProperty, Expression.Property(original, propertyInfo)
)
);
transferExpressions.Add(
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 propertys directly
// assigned without boxing, new instance creation or anything like that. // assigned without boxing, new instance creation or anything like that.
generatePropertyBasedComplexTypeTransferExpressions( generatePropertyBasedComplexTypeTransferExpressions(
propertyType, propertyType,
Expression.Property(original, propertyInfo), originalProperty,
Expression.Property(clone, propertyInfo), clonedProperty,
variables, variables,
transferExpressions transferExpressions
); );
transferExpressions.Add(
Expression.Assign(
Expression.Property(clone, propertyInfo),
clonedProperty
)
);
} else { } else {
generatePropertyBasedReferenceTypeTransferExpressions( generatePropertyBasedReferenceTypeTransferExpressions(
original, clone, transferExpressions, propertyInfo, propertyType original, clone, transferExpressions, variables, propertyInfo, propertyType
); );
} }
} }
@ -414,9 +434,17 @@ namespace Nuclex.Support.Cloning {
Expression original, Expression original,
Expression clone, Expression clone,
ICollection<Expression> transferExpressions, ICollection<Expression> transferExpressions,
ICollection<ParameterExpression> variables,
PropertyInfo propertyInfo, PropertyInfo propertyInfo,
Type propertyType Type propertyType
) { ) {
ParameterExpression originalProperty = Expression.Variable(propertyType);
variables.Add(originalProperty);
transferExpressions.Add(
Expression.Assign(originalProperty, Expression.Property(original, propertyInfo))
);
// Reference types and arrays require special care because they can be null, // Reference types and arrays require special care because they can be null,
// so gather the transfer expressions in a separate block for the null check // so gather the transfer expressions in a separate block for the null check
var propertyTransferExpressions = new List<Expression>(); var propertyTransferExpressions = new List<Expression>();
@ -431,7 +459,7 @@ namespace Nuclex.Support.Cloning {
// For primitive arrays, the Array.Clone() method is sufficient // For primitive arrays, the Array.Clone() method is sufficient
propertyClone = generatePropertyBasedPrimitiveArrayTransferExpressions( propertyClone = generatePropertyBasedPrimitiveArrayTransferExpressions(
propertyType, propertyType,
Expression.Property(original, propertyInfo), originalProperty,
propertyVariables, propertyVariables,
propertyTransferExpressions propertyTransferExpressions
); );
@ -439,7 +467,7 @@ namespace Nuclex.Support.Cloning {
// Arrays of complex types require manual cloning // Arrays of complex types require manual cloning
propertyClone = generatePropertyBasedComplexArrayTransferExpressions( propertyClone = generatePropertyBasedComplexArrayTransferExpressions(
propertyType, propertyType,
Expression.Property(original, propertyInfo), originalProperty,
propertyVariables, propertyVariables,
propertyTransferExpressions propertyTransferExpressions
); );
@ -473,12 +501,10 @@ namespace Nuclex.Support.Cloning {
Expression.Call( Expression.Call(
Expression.Call( Expression.Call(
getOrCreateClonerMethodInfo, getOrCreateClonerMethodInfo,
Expression.Call( Expression.Call(originalProperty, getTypeMethodInfo)
Expression.Property(original, propertyInfo), getTypeMethodInfo
)
), ),
invokeMethodInfo, invokeMethodInfo,
Expression.Property(original, propertyInfo) originalProperty
), ),
propertyType propertyType
) )
@ -491,15 +517,13 @@ namespace Nuclex.Support.Cloning {
transferExpressions.Add( transferExpressions.Add(
Expression.IfThen( Expression.IfThen(
Expression.NotEqual( Expression.NotEqual(
Expression.Property(original, propertyInfo), Expression.Constant(null) originalProperty, Expression.Constant(null)
), ),
Expression.Block(propertyVariables, propertyTransferExpressions) Expression.Block(propertyVariables, propertyTransferExpressions)
) )
); );
} }
#endif
} }
} // namespace Nuclex.Support.Cloning } // namespace Nuclex.Support.Cloning

View File

@ -172,7 +172,6 @@ namespace Nuclex.Support.Cloning {
} }
#endif #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>
@ -192,7 +191,6 @@ namespace Nuclex.Support.Cloning {
HierarchicalReferenceType clone = this.cloneFactory.DeepClone(original, true); HierarchicalReferenceType clone = this.cloneFactory.DeepClone(original, true);
VerifyClone(original, clone, isDeepClone: true, isPropertyBasedClone: true); VerifyClone(original, clone, isDeepClone: true, isPropertyBasedClone: true);
} }
#endif
/// <summary>Clone factory being tested</summary> /// <summary>Clone factory being tested</summary>
private ICloneFactory cloneFactory; private ICloneFactory cloneFactory;

View File

@ -35,6 +35,8 @@ namespace Nuclex.Support.Cloning {
static ExpressionTreeCloner() { static ExpressionTreeCloner() {
shallowFieldBasedCloners = new ConcurrentDictionary<Type, Func<object, object>>(); shallowFieldBasedCloners = new ConcurrentDictionary<Type, Func<object, object>>();
deepFieldBasedCloners = new ConcurrentDictionary<Type, Func<object, object>>(); deepFieldBasedCloners = new ConcurrentDictionary<Type, Func<object, object>>();
shallowPropertyBasedCloners = new ConcurrentDictionary<Type, Func<object, object>>();
deepPropertyBasedCloners = new ConcurrentDictionary<Type, Func<object, object>>();
} }
/// <summary> /// <summary>
@ -55,12 +57,13 @@ namespace Nuclex.Support.Cloning {
return default(TCloned); return default(TCloned);
} }
Func<object, object> cloner;
if(usePropertyBasedClone) { if(usePropertyBasedClone) {
throw new NotImplementedException("Not implemented yet"); cloner = getOrCreateDeepPropertyBasedCloner(typeof(TCloned));
} else { } else {
Func<object, object> cloner = getOrCreateDeepFieldBasedCloner(typeof(TCloned)); cloner = getOrCreateDeepFieldBasedCloner(typeof(TCloned));
return (TCloned)cloner(objectToCloneAsObject);
} }
return (TCloned)cloner(objectToCloneAsObject);
} }
/// <summary> /// <summary>
@ -164,17 +167,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 Action<TCloned, TCloned> CreateReferenceCopier<TCloned>(bool deepClone)
where TCloned : class {
throw new NotImplementedException();
}
#endif #endif
/// <summary> /// <summary>
@ -211,6 +203,41 @@ namespace Nuclex.Support.Cloning {
return cloner; return cloner;
} }
/// <summary>
/// Retrieves the existing clone method for the specified type or compiles one if
/// none exists for the type yet
/// </summary>
/// <param name="clonedType">Type for which a clone method will be retrieved</param>
/// <returns>The clone method for the specified type</returns>
private static Func<object, object> getOrCreateShallowPropertyBasedCloner(Type clonedType) {
Func<object, object> cloner;
if(!shallowPropertyBasedCloners.TryGetValue(clonedType, out cloner)) {
throw new NotImplementedException();
//cloner = createShallowPropertyBasedCloner(clonedType);
shallowPropertyBasedCloners.TryAdd(clonedType, cloner);
}
return cloner;
}
/// <summary>
/// Retrieves the existing clone method for the specified type or compiles one if
/// none exists for the type yet
/// </summary>
/// <param name="clonedType">Type for which a clone method will be retrieved</param>
/// <returns>The clone method for the specified type</returns>
private static Func<object, object> getOrCreateDeepPropertyBasedCloner(Type clonedType) {
Func<object, object> cloner;
if(!deepPropertyBasedCloners.TryGetValue(clonedType, out cloner)) {
cloner = createDeepPropertyBasedCloner(clonedType);
deepPropertyBasedCloners.TryAdd(clonedType, cloner);
}
return cloner;
}
/// <summary>Compiled cloners that perform shallow clone operations</summary> /// <summary>Compiled cloners that perform shallow clone operations</summary>
private static ConcurrentDictionary<Type, Func<object, object>> shallowFieldBasedCloners; private static ConcurrentDictionary<Type, Func<object, object>> shallowFieldBasedCloners;
/// <summary>Compiled cloners that perform deep clone operations</summary> /// <summary>Compiled cloners that perform deep clone operations</summary>

View File

@ -120,7 +120,11 @@ namespace Nuclex.Support.Cloning {
/// <returns>A clone of the original instance</returns> /// <returns>A clone of the original instance</returns>
private static object shallowCloneComplexFieldBased(object original) { private static object shallowCloneComplexFieldBased(object original) {
Type originalType = original.GetType(); Type originalType = original.GetType();
#if (XBOX360 || WINDOWS_PHONE)
object clone = Activator.CreateInstance(originalType);
#else
object clone = FormatterServices.GetUninitializedObject(originalType); object clone = FormatterServices.GetUninitializedObject(originalType);
#endif
FieldInfo[] fieldInfos = originalType.GetFields( FieldInfo[] fieldInfos = originalType.GetFields(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.NonPublic |
@ -197,7 +201,11 @@ namespace Nuclex.Support.Cloning {
/// <returns>A clone of the original instance</returns> /// <returns>A clone of the original instance</returns>
private static object deepCloneComplexFieldBased(object original) { private static object deepCloneComplexFieldBased(object original) {
Type originalType = original.GetType(); Type originalType = original.GetType();
#if (XBOX360 || WINDOWS_PHONE)
object clone = Activator.CreateInstance(originalType);
#else
object clone = FormatterServices.GetUninitializedObject(originalType); object clone = FormatterServices.GetUninitializedObject(originalType);
#endif
FieldInfo[] fieldInfos = originalType.GetFields( FieldInfo[] fieldInfos = originalType.GetFields(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.NonPublic |