Expression tree cloner is now able to clone arrays of primitive types

git-svn-id: file:///srv/devel/repo-conversion/nusu@231 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2012-02-03 20:39:07 +00:00
parent 4743b5d056
commit 1307976597
2 changed files with 21 additions and 9 deletions

View File

@ -66,7 +66,7 @@ namespace Nuclex.Support.Cloning {
Assert.AreSame(original[0], clone[0]);
}
//#endif
/// <summary>Verifies that deep clones of arrays can be made</summary>
[Test]
public void DeepClonesOfArraysCanBeMade() {
@ -79,7 +79,7 @@ namespace Nuclex.Support.Cloning {
Assert.AreEqual(original[0].TestField, clone[0].TestField);
Assert.AreEqual(original[0].TestProperty, clone[0].TestProperty);
}
//#if false
/// <summary>Verifies that deep clones of a generic list can be made</summary>
[Test]
public void GenericListsCanBeCloned() {

View File

@ -135,14 +135,10 @@ namespace Nuclex.Support.Cloning {
}
/// <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>
/// <typeparam name="TCloned">Type for which a clone method will be created</typeparam>
/// <returns>A method that clones an object of the provided type</returns>
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(TCloned), "original");
ParameterExpression clone = Expression.Variable(typeof(TCloned), "clone");
@ -150,12 +146,28 @@ namespace Nuclex.Support.Cloning {
var transferExpressions = new List<Expression>();
if(clonedType.IsPrimitive || (clonedType == typeof(string))) {
transferExpressions.Add(Expression.Assign(clone, original));
transferExpressions.Add(original); // primitives are copied on assignment
} else if(clonedType.IsArray) {
throw new NotImplementedException("Not implemented yet");
Type elementType = clonedType.GetElementType();
if(elementType.IsPrimitive || (elementType == typeof(string))) {
MethodInfo arrayCloneMethodInfo = typeof(Array).GetMethod("Clone");
transferExpressions.Add(
Expression.Convert(
Expression.Call(original, arrayCloneMethodInfo),
clonedType
)
);
} else {
}
} else {
transferExpressions.Add(Expression.Assign(clone, Expression.New(clonedType)));
FieldInfo[] fieldInfos = clonedType.GetFields(
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.FlattenHierarchy
);
for(int index = 0; index < fieldInfos.Length; ++index) {
FieldInfo fieldInfo = fieldInfos[index];
Type fieldType = fieldInfo.FieldType;