#region CPL License /* Nuclex Framework Copyright (C) 2002-2010 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #endregion #if !(XBOX360 || WINDOWS_PHONE) using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq.Expressions; using System.Reflection; namespace Nuclex.Support.Cloning { /// An action that takes its arguments as references to a structure /// Type of the first argument to the method /// Type of the second argument to the method /// First argument to the method /// Second argument to the method public delegate void ReferenceAction(ref TFirst first, ref TSecond second) where TFirst : struct where TSecond : struct; /// /// Cloning factory which uses expression trees to improve performance when cloning /// is a high-frequency action. /// public class ExpressionTreeCloner : ICloneFactory { /// Initializes the static members of the expression tree cloner static ExpressionTreeCloner() { shallowCloners = new ConcurrentDictionary>(); deepCloners = new ConcurrentDictionary>(); } /// /// Creates a deep clone of the specified object, also creating clones of all /// child objects being referenced /// /// Type of the object that will be cloned /// Object that will be cloned /// /// Whether to clone the object based on its properties only /// /// A deep clone of the provided object public static TCloned DeepClone( TCloned objectToClone, bool usePropertyBasedClone ) { object objectToCloneAsObject = objectToClone; if(objectToCloneAsObject == null) { return default(TCloned); } if(usePropertyBasedClone) { throw new NotImplementedException("Not implemented yet"); } else { Func cloner = getOrCreateDeepFieldBasedCloner(typeof(TCloned)); return (TCloned)cloner(objectToCloneAsObject); } } /// /// Creates a shallow clone of the specified object, reusing any referenced objects /// /// Type of the object that will be cloned /// Object that will be cloned /// /// Whether to clone the object based on its properties only /// /// A shallow clone of the provided object public static TCloned ShallowClone( TCloned objectToClone, bool usePropertyBasedClone ) { throw new NotImplementedException("Not implemented yet"); } /// /// Creates a deep clone of the specified object, also creating clones of all /// child objects being referenced /// /// Type of the object that will be cloned /// Object that will be cloned /// /// Whether to clone the object based on its properties only /// /// A deep clone of the provided object TCloned ICloneFactory.DeepClone( TCloned objectToClone, bool usePropertyBasedClone ) { return ExpressionTreeCloner.DeepClone(objectToClone, usePropertyBasedClone); } /// /// Creates a shallow clone of the specified object, reusing any referenced objects /// /// Type of the object that will be cloned /// Object that will be cloned /// /// Whether to clone the object based on its properties only /// /// A shallow clone of the provided object TCloned ICloneFactory.ShallowClone( TCloned objectToClone, bool usePropertyBasedClone ) { return ExpressionTreeCloner.ShallowClone(objectToClone, usePropertyBasedClone); } /// /// Retrieves the existing clone method for the specified type or compiles one if /// none exists for the type yet /// /// Type for which a clone method will be retrieved /// The clone method for the specified type private static Func getOrCreateDeepFieldBasedCloner(Type clonedType) { Func cloner; if(!deepCloners.TryGetValue(clonedType, out cloner)) { cloner = createDeepFieldBasedCloner(clonedType); deepCloners.TryAdd(clonedType, cloner); } return cloner; } /// /// Generates state transfer expressions to copy an array of primitive types /// /// Type of array that will be cloned /// Variable expression for the original array /// Receives variables used by the transfer expressions /// Receives the generated transfer expressions /// The variable holding the cloned array private static Expression generatePrimitiveArrayTransferExpressions( Type clonedType, Expression original, ICollection variables, ICollection transferExpressions ) { MethodInfo arrayCloneMethodInfo = typeof(Array).GetMethod("Clone"); return Expression.Convert( Expression.Call( Expression.Convert(original, typeof(Array)), arrayCloneMethodInfo ), clonedType ); } /// /// Generates state transfer expressions to copy an array of complex types /// /// Type of array that will be cloned /// Variable expression for the original array /// Receives variables used by the transfer expressions /// Receives the generated transfer expressions /// The variable holding the cloned array private static ParameterExpression generateComplexArrayTransferExpressions( Type clonedType, Expression original, IList variables, ICollection transferExpressions ) { // We need a temporary variable in order to transfer the elements of the array ParameterExpression clone = Expression.Variable(clonedType); variables.Add(clone); int dimensionCount = clonedType.GetArrayRank(); int baseVariableIndex = variables.Count; Type elementType = clonedType.GetElementType(); // Retrieve the length of each of the array's dimensions MethodInfo arrayGetLengthMethodInfo = typeof(Array).GetMethod("GetLength"); var lengths = new List(); for(int index = 0; index < dimensionCount; ++index) { ParameterExpression length = Expression.Variable(typeof(int)); variables.Add(length); lengths.Add(length); transferExpressions.Add( Expression.Assign( length, Expression.Call( original, arrayGetLengthMethodInfo, Expression.Constant(index) ) ) ); } transferExpressions.Add( Expression.Assign( clone, Expression.NewArrayBounds(elementType, lengths) ) ); return clone; } /// Generates state transfer expressions to copy a complex type /// Complex type that will be cloned /// Variable expression for the original instance /// Variable expression for the cloned instance /// Receives variables used by the transfer expressions /// Receives the generated transfer expressions private static void generateComplexTypeTransferExpressions( Type clonedType, // Actual, concrete type (not declared type) Expression original, // Expected to be an object Expression clone, // As actual, concrete type IList variables, ICollection transferExpressions ) { // Enumerate all of the type's fields and generate transfer expressions for each 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; if(fieldType.IsPrimitive || (fieldType == typeof(string))) { // Primitive types and strings can be transferred by simple assignment transferExpressions.Add( Expression.Assign( Expression.Field(clone, fieldInfo), Expression.Field(original, fieldInfo) ) ); } else if(fieldType.IsValueType) { // A nested value type is part of the parent and will have its fields directly // assigned without boxing, new instance creation or anything like that. generateComplexTypeTransferExpressions( fieldType, Expression.Field(original, fieldInfo), Expression.Field(clone, fieldInfo), variables, transferExpressions ); } else { // 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 Expression fieldClone; var fieldTransferExpressions = new List(); var fieldVariables = new List(); if(fieldType.IsArray) { // Arrays need to be cloned element-by-element Type elementType = fieldType.GetElementType(); if(elementType.IsPrimitive || (elementType == typeof(string))) { // For primitive arrays, the Array.Clone() method is sufficient fieldClone = generatePrimitiveArrayTransferExpressions( fieldType, Expression.Field(original, fieldInfo), fieldVariables, fieldTransferExpressions ); } else { // Arrays of complex types require manual cloning fieldClone = generateComplexArrayTransferExpressions( fieldType, Expression.Field(original, fieldInfo), fieldVariables, fieldTransferExpressions ); } // Add the assignment to the transfer expressions. The array transfer expression // generator will either have set up a temporary variable to hold the array or // returned the conversion expression straight away fieldTransferExpressions.Add( Expression.Assign(Expression.Field(clone, fieldInfo), fieldClone) ); } else { // Complex types are cloned by checking their actual, concrete type (fields // may be typed to an interface or base class) and requesting a cloner for that // type during runtime MethodInfo getOrCreateClonerMethodInfo = typeof(ExpressionTreeCloner).GetMethod( "getOrCreateDeepFieldBasedCloner", BindingFlags.NonPublic | BindingFlags.Static ); MethodInfo getTypeMethodInfo = typeof(object).GetMethod("GetType"); MethodInfo invokeMethodInfo = typeof(Func).GetMethod("Invoke"); // Generate expressions to do this: // clone.SomeField = getOrCreateDeepFieldBasedCloner( // original.SomeField.GetType() // ).Invoke(original.SomeField); fieldTransferExpressions.Add( Expression.Assign( Expression.Field(clone, fieldInfo), Expression.Convert( Expression.Call( Expression.Call( getOrCreateClonerMethodInfo, Expression.Call( Expression.Field(original, fieldInfo), getTypeMethodInfo ) ), invokeMethodInfo, Expression.Field(original, fieldInfo) ), fieldType ) ) ); } // Wrap up the generated array or complex reference type transfer expressions // in a null check so the field is skipped if it is not holding an instance. transferExpressions.Add( Expression.IfThen( Expression.NotEqual( Expression.Field(original, fieldInfo), Expression.Constant(null) ), Expression.Block(fieldVariables, fieldTransferExpressions) ) ); } } } /// Compiles a method that creates a clone of an object /// Type for which a clone method will be created /// A method that clones an object of the provided type /// /// /// The 'null' check is supposed to take place before running the cloner. This /// avoids having redundant 'null' checks on nested types - first before calling /// GetType() on the field to be cloned and second when runner the matching /// cloner for the field. /// /// /// This design also enables the cloning of nested value types (which can never /// be null) without any null check whatsoever. /// /// private static Func createDeepFieldBasedCloner(Type clonedType) { ParameterExpression original = Expression.Parameter(typeof(object), "original"); var transferExpressions = new List(); var variables = new List(); if(clonedType.IsPrimitive || (clonedType == typeof(string))) { // Primitives and strings are copied on direct assignment transferExpressions.Add(original); } else if(clonedType.IsArray) { // Arrays need to be cloned element-by-element Type elementType = clonedType.GetElementType(); if(elementType.IsPrimitive || (elementType == typeof(string))) { // For primitive arrays, the Array.Clone() method is sufficient transferExpressions.Add( generatePrimitiveArrayTransferExpressions( clonedType, original, variables, transferExpressions ) ); } else { // To access the fields of the original type, we need it to be of the actual // type instead of an object, so perform a downcast ParameterExpression typedOriginal = Expression.Variable(clonedType); variables.Add(typedOriginal); transferExpressions.Add( Expression.Assign(typedOriginal, Expression.Convert(original, clonedType)) ); // Arrays of complex types require manual cloning transferExpressions.Add( generateComplexArrayTransferExpressions( clonedType, typedOriginal, variables, transferExpressions ) ); } } else { // We need a variable to hold the clone because due to the assignments it // won't be last in the block when we're finished ParameterExpression clone = Expression.Variable(clonedType); variables.Add(clone); // Give it a new instance of the type being cloned transferExpressions.Add(Expression.Assign(clone, Expression.New(clonedType))); // To access the fields of the original type, we need it to be of the actual // type instead of an object, so perform a downcast ParameterExpression typedOriginal = Expression.Variable(clonedType); variables.Add(typedOriginal); transferExpressions.Add( Expression.Assign(typedOriginal, Expression.Convert(original, clonedType)) ); // Generate the expressions required to transfer the type field by field generateComplexTypeTransferExpressions( clonedType, typedOriginal, clone, variables, transferExpressions ); // Make sure the clone is the last thing in the block to set the return value transferExpressions.Add(clone); } // Turn all transfer expressions into a single block if necessary Expression resultExpression; if((transferExpressions.Count == 1) && (variables.Count == 0)) { resultExpression = transferExpressions[0]; } else { resultExpression = Expression.Block(variables, transferExpressions); } // Value types require manual boxing if(clonedType.IsValueType) { resultExpression = Expression.Convert(resultExpression, typeof(object)); } Expression> expression = Expression.Lambda>( resultExpression, original ); return expression.Compile(); } #if false /// /// Transfers the state of one object into another, creating clones of referenced objects /// /// Type of the object whose sate will be transferred /// Original instance the state will be taken from /// Target instance the state will be written to /// Whether to perform a property-based state copy public void DeepCopyState(TState original, TState target, bool propertyBased) where TState : class { throw new NotImplementedException(); } /// /// Transfers the state of one object into another, creating clones of referenced objects /// /// Type of the object whose sate will be transferred /// Original instance the state will be taken from /// Target instance the state will be written to /// Whether to perform a property-based state copy public void DeepCopyState(ref TState original, ref TState target, bool propertyBased) where TState : struct { throw new NotImplementedException(); } /// Transfers the state of one object into another /// Type of the object whose sate will be transferred /// Original instance the state will be taken from /// Target instance the state will be written to /// Whether to perform a property-based state copy public void ShallowCopyState(TState original, TState target, bool propertyBased) where TState : class { throw new NotImplementedException(); } /// Transfers the state of one object into another /// Type of the object whose sate will be transferred /// Original instance the state will be taken from /// Target instance the state will be written to /// Whether to perform a property-based state copy public void ShallowCopyState(ref TState original, ref TState target, bool propertyBased) where TState : struct { throw new NotImplementedException(); } /// /// Compiles a method that copies the state of one object into another object /// /// Type of object whose state will be copied /// Whether to create clones of the referenced objects /// A method that copies the state from one object into another object public static Action CreateReferenceCopier(bool deepClone) where TCloned : class { throw new NotImplementedException(); } /// /// Compiles a method that copies the state of one object into another object /// /// Type of object whose state will be copied /// Whether to create clones of the referenced objects /// A method that copies the state from one object into another object public static ReferenceAction CreateValueCopier(bool deepClone) where TCloned : struct { throw new NotImplementedException(); } /// Compiles a method that creates a clone of an object /// Type of object that will be cloned /// Whether to create clones of the referenced objects /// A method that clones an object of the provided type public static Func CreateCloner(bool deepClone) where TCloned : class, new() { throw new NotImplementedException(); } #endif /// Compiled cloners that perform shallow clone operations private static ConcurrentDictionary> shallowCloners; /// Compiled cloners that perform deep clone operations private static ConcurrentDictionary> deepCloners; } } // namespace Nuclex.Support.Cloning #endif // !(XBOX360 || WINDOWS_PHONE)