From 13079765979ebdadf3070c8793d79500555cb87c Mon Sep 17 00:00:00 2001 From: Markus Ewald Date: Fri, 3 Feb 2012 20:39:07 +0000 Subject: [PATCH] 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 --- Source/Cloning/ExpressionTreeCloner.Test.cs | 4 ++-- Source/Cloning/ExpressionTreeCloner.cs | 26 +++++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Source/Cloning/ExpressionTreeCloner.Test.cs b/Source/Cloning/ExpressionTreeCloner.Test.cs index 98a76f8..1d20b14 100644 --- a/Source/Cloning/ExpressionTreeCloner.Test.cs +++ b/Source/Cloning/ExpressionTreeCloner.Test.cs @@ -66,7 +66,7 @@ namespace Nuclex.Support.Cloning { Assert.AreSame(original[0], clone[0]); } - +//#endif /// Verifies that deep clones of arrays can be made [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 /// Verifies that deep clones of a generic list can be made [Test] public void GenericListsCanBeCloned() { diff --git a/Source/Cloning/ExpressionTreeCloner.cs b/Source/Cloning/ExpressionTreeCloner.cs index 2a67817..f72beac 100644 --- a/Source/Cloning/ExpressionTreeCloner.cs +++ b/Source/Cloning/ExpressionTreeCloner.cs @@ -135,14 +135,10 @@ namespace Nuclex.Support.Cloning { } /// Compiles a method that creates a clone of an object - /// Type for which a clone method will be created + /// Type for which a clone method will be created /// A method that clones an object of the provided type private static Func createDeepFieldBasedCloner() { 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(); 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;