Expression tree cloner now is able to clone complex types (non-recursively, at the moment)
git-svn-id: file:///srv/devel/repo-conversion/nusu@230 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
776a0749fd
commit
4743b5d056
|
@ -51,8 +51,8 @@ namespace Nuclex.Support.Cloning {
|
|||
TestReferenceType clone = this.cloneFactory.DeepClone(original, false);
|
||||
|
||||
Assert.AreNotSame(original, clone);
|
||||
//Assert.AreEqual(original.TestField, clone.TestField);
|
||||
//Assert.AreEqual(original.TestProperty, clone.TestProperty);
|
||||
Assert.AreEqual(original.TestField, clone.TestField);
|
||||
Assert.AreEqual(original.TestProperty, clone.TestProperty);
|
||||
}
|
||||
|
||||
#if false
|
||||
|
|
|
@ -45,8 +45,8 @@ namespace Nuclex.Support.Cloning {
|
|||
|
||||
/// <summary>Initializes the static members of the expression tree cloner</summary>
|
||||
static ExpressionTreeCloner() {
|
||||
shallowCloners = new ConcurrentDictionary<Type, Func<object, object>>();
|
||||
deepCloners = new ConcurrentDictionary<Type, Func<object, object>>();
|
||||
shallowCloners = new ConcurrentDictionary<Type, Delegate>();
|
||||
deepCloners = new ConcurrentDictionary<Type, Delegate>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -65,8 +65,8 @@ namespace Nuclex.Support.Cloning {
|
|||
if(usePropertyBasedClone) {
|
||||
throw new NotImplementedException("Not implemented yet");
|
||||
} else {
|
||||
Func<object, object> cloner = getOrCreateDeepFieldBasedCloner(typeof(TCloned));
|
||||
return (TCloned)cloner(objectToClone);
|
||||
Func<TCloned, TCloned> cloner = getOrCreateDeepFieldBasedCloner<TCloned>();
|
||||
return cloner(objectToClone);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -120,43 +120,42 @@ namespace Nuclex.Support.Cloning {
|
|||
/// Retrieves the existing clone method for the specified type or compiles one if
|
||||
/// none exists for the type yet
|
||||
/// </summary>
|
||||
/// <param name="type">Type for which a clone method will be retrieved</param>
|
||||
/// <typeparam name="TCloned">Type for which a clone method will be retrieved</typeparam>
|
||||
/// <returns>The clone method for the specified type</returns>
|
||||
private static Func<object, object> getOrCreateDeepFieldBasedCloner(Type type) {
|
||||
Func<object, object> cloner;
|
||||
if(!deepCloners.TryGetValue(type, out cloner)) {
|
||||
cloner = createDeepFieldBasedCloner(type);
|
||||
deepCloners.TryAdd(type, cloner);
|
||||
}
|
||||
|
||||
private static Func<TCloned, TCloned> getOrCreateDeepFieldBasedCloner<TCloned>() {
|
||||
Type clonedType = typeof(TCloned);
|
||||
Delegate clonerAsDelegate;
|
||||
if(deepCloners.TryGetValue(clonedType, out clonerAsDelegate)) {
|
||||
return (Func<TCloned, TCloned>)clonerAsDelegate;
|
||||
} else {
|
||||
Func<TCloned, TCloned> cloner = createDeepFieldBasedCloner<TCloned>();
|
||||
deepCloners.TryAdd(clonedType, cloner);
|
||||
return cloner;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Compiles a method that creates a clone of an object</summary>
|
||||
/// <param name="type">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 type) {
|
||||
FieldInfo[] fieldInfos = type.GetFields(
|
||||
private static Func<TCloned, TCloned> createDeepFieldBasedCloner<TCloned>() {
|
||||
Type clonedType = typeof(TCloned);
|
||||
FieldInfo[] fieldInfos = clonedType.GetFields(
|
||||
BindingFlags.Public | BindingFlags.NonPublic |
|
||||
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
||||
);
|
||||
|
||||
ParameterExpression original = Expression.Parameter(typeof(object), "original");
|
||||
ParameterExpression clone = Expression.Variable(typeof(object), "clone");
|
||||
//ParameterExpression typedOriginal = Expression.Variable(type, "typedOriginal");
|
||||
ParameterExpression original = Expression.Parameter(typeof(TCloned), "original");
|
||||
ParameterExpression clone = Expression.Variable(typeof(TCloned), "clone");
|
||||
|
||||
var transferExpressions = new List<Expression>();
|
||||
|
||||
if(type.IsPrimitive || (type == typeof(string))) {
|
||||
if(clonedType.IsPrimitive || (clonedType == typeof(string))) {
|
||||
transferExpressions.Add(Expression.Assign(clone, original));
|
||||
} else if(type.IsArray) {
|
||||
//throw new NotImplementedException("Not implemented yet");
|
||||
} else if(clonedType.IsArray) {
|
||||
throw new NotImplementedException("Not implemented yet");
|
||||
} else {
|
||||
transferExpressions.Add(Expression.Assign(clone, Expression.New(type)));
|
||||
/*
|
||||
transferExpressions.Add(
|
||||
Expression.Assign(typedOriginal, Expression.Convert(original, type))
|
||||
);
|
||||
transferExpressions.Add(Expression.Assign(clone, Expression.New(clonedType)));
|
||||
|
||||
for(int index = 0; index < fieldInfos.Length; ++index) {
|
||||
FieldInfo fieldInfo = fieldInfos[index];
|
||||
Type fieldType = fieldInfo.FieldType;
|
||||
|
@ -165,16 +164,17 @@ namespace Nuclex.Support.Cloning {
|
|||
transferExpressions.Add(
|
||||
Expression.Assign(
|
||||
Expression.Field(clone, fieldInfo),
|
||||
Expression.Field(typedOriginal, fieldInfo)
|
||||
Expression.Field(original, fieldInfo)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
transferExpressions.Add(clone);
|
||||
}
|
||||
|
||||
Expression<Func<object, object>> expression =
|
||||
Expression.Lambda<Func<object, object>>(
|
||||
Expression<Func<TCloned, TCloned>> expression =
|
||||
Expression.Lambda<Func<TCloned, TCloned>>(
|
||||
Expression.Block(
|
||||
new[] { clone },
|
||||
transferExpressions
|
||||
|
@ -263,9 +263,9 @@ namespace Nuclex.Support.Cloning {
|
|||
#endif
|
||||
|
||||
/// <summary>Compiled cloners that perform shallow clone operations</summary>
|
||||
private static ConcurrentDictionary<Type, Func<object, object>> shallowCloners;
|
||||
private static ConcurrentDictionary<Type, Delegate> shallowCloners;
|
||||
/// <summary>Compiled cloners that perform deep clone operations</summary>
|
||||
private static ConcurrentDictionary<Type, Func<object, object>> deepCloners;
|
||||
private static ConcurrentDictionary<Type, Delegate> deepCloners;
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user