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);
|
TestReferenceType clone = this.cloneFactory.DeepClone(original, false);
|
||||||
|
|
||||||
Assert.AreNotSame(original, clone);
|
Assert.AreNotSame(original, clone);
|
||||||
//Assert.AreEqual(original.TestField, clone.TestField);
|
Assert.AreEqual(original.TestField, clone.TestField);
|
||||||
//Assert.AreEqual(original.TestProperty, clone.TestProperty);
|
Assert.AreEqual(original.TestProperty, clone.TestProperty);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if false
|
#if false
|
||||||
|
|
|
@ -45,8 +45,8 @@ namespace Nuclex.Support.Cloning {
|
||||||
|
|
||||||
/// <summary>Initializes the static members of the expression tree cloner</summary>
|
/// <summary>Initializes the static members of the expression tree cloner</summary>
|
||||||
static ExpressionTreeCloner() {
|
static ExpressionTreeCloner() {
|
||||||
shallowCloners = new ConcurrentDictionary<Type, Func<object, object>>();
|
shallowCloners = new ConcurrentDictionary<Type, Delegate>();
|
||||||
deepCloners = new ConcurrentDictionary<Type, Func<object, object>>();
|
deepCloners = new ConcurrentDictionary<Type, Delegate>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -65,8 +65,8 @@ namespace Nuclex.Support.Cloning {
|
||||||
if(usePropertyBasedClone) {
|
if(usePropertyBasedClone) {
|
||||||
throw new NotImplementedException("Not implemented yet");
|
throw new NotImplementedException("Not implemented yet");
|
||||||
} else {
|
} else {
|
||||||
Func<object, object> cloner = getOrCreateDeepFieldBasedCloner(typeof(TCloned));
|
Func<TCloned, TCloned> cloner = getOrCreateDeepFieldBasedCloner<TCloned>();
|
||||||
return (TCloned)cloner(objectToClone);
|
return cloner(objectToClone);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,43 +120,42 @@ namespace Nuclex.Support.Cloning {
|
||||||
/// Retrieves the existing clone method for the specified type or compiles one if
|
/// Retrieves the existing clone method for the specified type or compiles one if
|
||||||
/// none exists for the type yet
|
/// none exists for the type yet
|
||||||
/// </summary>
|
/// </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>
|
/// <returns>The clone method for the specified type</returns>
|
||||||
private static Func<object, object> getOrCreateDeepFieldBasedCloner(Type type) {
|
private static Func<TCloned, TCloned> getOrCreateDeepFieldBasedCloner<TCloned>() {
|
||||||
Func<object, object> cloner;
|
Type clonedType = typeof(TCloned);
|
||||||
if(!deepCloners.TryGetValue(type, out cloner)) {
|
Delegate clonerAsDelegate;
|
||||||
cloner = createDeepFieldBasedCloner(type);
|
if(deepCloners.TryGetValue(clonedType, out clonerAsDelegate)) {
|
||||||
deepCloners.TryAdd(type, cloner);
|
return (Func<TCloned, TCloned>)clonerAsDelegate;
|
||||||
}
|
} else {
|
||||||
|
Func<TCloned, TCloned> cloner = createDeepFieldBasedCloner<TCloned>();
|
||||||
|
deepCloners.TryAdd(clonedType, cloner);
|
||||||
return cloner;
|
return cloner;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Compiles a method that creates a clone of an object</summary>
|
/// <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>
|
/// <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>
|
/// <returns>A method that clones an object of the provided type</returns>
|
||||||
private static Func<object, object> createDeepFieldBasedCloner(Type type) {
|
private static Func<TCloned, TCloned> createDeepFieldBasedCloner<TCloned>() {
|
||||||
FieldInfo[] fieldInfos = type.GetFields(
|
Type clonedType = typeof(TCloned);
|
||||||
|
FieldInfo[] fieldInfos = clonedType.GetFields(
|
||||||
BindingFlags.Public | BindingFlags.NonPublic |
|
BindingFlags.Public | BindingFlags.NonPublic |
|
||||||
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
||||||
);
|
);
|
||||||
|
|
||||||
ParameterExpression original = Expression.Parameter(typeof(object), "original");
|
ParameterExpression original = Expression.Parameter(typeof(TCloned), "original");
|
||||||
ParameterExpression clone = Expression.Variable(typeof(object), "clone");
|
ParameterExpression clone = Expression.Variable(typeof(TCloned), "clone");
|
||||||
//ParameterExpression typedOriginal = Expression.Variable(type, "typedOriginal");
|
|
||||||
|
|
||||||
var transferExpressions = new List<Expression>();
|
var transferExpressions = new List<Expression>();
|
||||||
|
|
||||||
if(type.IsPrimitive || (type == typeof(string))) {
|
if(clonedType.IsPrimitive || (clonedType == typeof(string))) {
|
||||||
transferExpressions.Add(Expression.Assign(clone, original));
|
transferExpressions.Add(Expression.Assign(clone, original));
|
||||||
} else if(type.IsArray) {
|
} else if(clonedType.IsArray) {
|
||||||
//throw new NotImplementedException("Not implemented yet");
|
throw new NotImplementedException("Not implemented yet");
|
||||||
} else {
|
} else {
|
||||||
transferExpressions.Add(Expression.Assign(clone, Expression.New(type)));
|
transferExpressions.Add(Expression.Assign(clone, Expression.New(clonedType)));
|
||||||
/*
|
|
||||||
transferExpressions.Add(
|
|
||||||
Expression.Assign(typedOriginal, Expression.Convert(original, type))
|
|
||||||
);
|
|
||||||
for(int index = 0; index < fieldInfos.Length; ++index) {
|
for(int index = 0; index < fieldInfos.Length; ++index) {
|
||||||
FieldInfo fieldInfo = fieldInfos[index];
|
FieldInfo fieldInfo = fieldInfos[index];
|
||||||
Type fieldType = fieldInfo.FieldType;
|
Type fieldType = fieldInfo.FieldType;
|
||||||
|
@ -165,16 +164,17 @@ namespace Nuclex.Support.Cloning {
|
||||||
transferExpressions.Add(
|
transferExpressions.Add(
|
||||||
Expression.Assign(
|
Expression.Assign(
|
||||||
Expression.Field(clone, fieldInfo),
|
Expression.Field(clone, fieldInfo),
|
||||||
Expression.Field(typedOriginal, fieldInfo)
|
Expression.Field(original, fieldInfo)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
transferExpressions.Add(clone);
|
||||||
}
|
}
|
||||||
|
|
||||||
Expression<Func<object, object>> expression =
|
Expression<Func<TCloned, TCloned>> expression =
|
||||||
Expression.Lambda<Func<object, object>>(
|
Expression.Lambda<Func<TCloned, TCloned>>(
|
||||||
Expression.Block(
|
Expression.Block(
|
||||||
new[] { clone },
|
new[] { clone },
|
||||||
transferExpressions
|
transferExpressions
|
||||||
|
@ -263,9 +263,9 @@ namespace Nuclex.Support.Cloning {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// <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>> shallowCloners;
|
private static ConcurrentDictionary<Type, Delegate> shallowCloners;
|
||||||
/// <summary>Compiled cloners that perform deep clone operations</summary>
|
/// <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