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

View File

@ -172,7 +172,6 @@ namespace Nuclex.Support.Cloning {
}
#endif
#if false
/// <summary>
/// Verifies that a property-based deep clone of a value type can be performed
/// </summary>
@ -192,7 +191,6 @@ namespace Nuclex.Support.Cloning {
HierarchicalReferenceType clone = this.cloneFactory.DeepClone(original, true);
VerifyClone(original, clone, isDeepClone: true, isPropertyBasedClone: true);
}
#endif
/// <summary>Clone factory being tested</summary>
private ICloneFactory cloneFactory;

View File

@ -35,6 +35,8 @@ namespace Nuclex.Support.Cloning {
static ExpressionTreeCloner() {
shallowFieldBasedCloners = 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>
@ -55,12 +57,13 @@ namespace Nuclex.Support.Cloning {
return default(TCloned);
}
Func<object, object> cloner;
if(usePropertyBasedClone) {
throw new NotImplementedException("Not implemented yet");
cloner = getOrCreateDeepPropertyBasedCloner(typeof(TCloned));
} else {
Func<object, object> cloner = getOrCreateDeepFieldBasedCloner(typeof(TCloned));
return (TCloned)cloner(objectToCloneAsObject);
cloner = getOrCreateDeepFieldBasedCloner(typeof(TCloned));
}
return (TCloned)cloner(objectToCloneAsObject);
}
/// <summary>
@ -164,17 +167,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 Action<TCloned, TCloned> CreateReferenceCopier<TCloned>(bool deepClone)
where TCloned : class {
throw new NotImplementedException();
}
#endif
/// <summary>
@ -211,6 +203,41 @@ namespace Nuclex.Support.Cloning {
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>
private static ConcurrentDictionary<Type, Func<object, object>> shallowFieldBasedCloners;
/// <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>
private static object shallowCloneComplexFieldBased(object original) {
Type originalType = original.GetType();
#if (XBOX360 || WINDOWS_PHONE)
object clone = Activator.CreateInstance(originalType);
#else
object clone = FormatterServices.GetUninitializedObject(originalType);
#endif
FieldInfo[] fieldInfos = originalType.GetFields(
BindingFlags.Public | BindingFlags.NonPublic |
@ -197,7 +201,11 @@ namespace Nuclex.Support.Cloning {
/// <returns>A clone of the original instance</returns>
private static object deepCloneComplexFieldBased(object original) {
Type originalType = original.GetType();
#if (XBOX360 || WINDOWS_PHONE)
object clone = Activator.CreateInstance(originalType);
#else
object clone = FormatterServices.GetUninitializedObject(originalType);
#endif
FieldInfo[] fieldInfos = originalType.GetFields(
BindingFlags.Public | BindingFlags.NonPublic |