private Func <ParameterExpression, Expression> CloneListElementDelegate(IExpressionBuilder expressionBuilder, CloneExpressionContext context) { Type elementType = context .SourceEntity .Type .GenericTypeArguments .Single(); PropertyInfo indexProperty = typeof(IList <>) .MakeGenericType(elementType) .GetProperty(IndexPropertyName); MethodInfo addMethod = typeof(ICollection <>) .MakeGenericType(elementType) .GetMethod(nameof(ICollection <object> .Add)); if (PrimitiveTypes.Contains(elementType)) { return (iterator) => { Expression getElementExpression = ExpressionFactory.Call(context.SourceEntity, indexProperty.GetMethod, iterator); return ExpressionFactory.Call(context.TargetEntity, addMethod, getElementExpression); } } ; else { return (iterator) => { Expression getElementExpression = ExpressionFactory.Call(context.SourceEntity, indexProperty.GetMethod, iterator); MethodInfo cloneMethod = CloneMethod.MakeGenericMethod(elementType); Expression clonedElement = ExpressionFactory.Call(context.CloneFactory, cloneMethod, getElementExpression); return ExpressionFactory.Call(context.TargetEntity, addMethod, clonedElement); } }; }
private Func <ParameterExpression, Expression> CloneArrayElementDelegate(CloneExpressionContext context) { Type elementType = context .SourceEntity .Type .GetElementType(); PropertyInfo indexProperty = typeof(IList <>) .MakeGenericType(elementType) .GetProperty(IndexPropertyName); if (PrimitiveTypes.Contains(elementType)) { return (iterator) => { Expression getElementExpression = ExpressionFactory.Call(context.SourceEntity, indexProperty.GetMethod, iterator); return ExpressionFactory.Call(context.TargetEntity, indexProperty.SetMethod, iterator, getElementExpression); } } ; else { return (iterator) => { Expression getElementExpression = ExpressionFactory.Call(context.SourceEntity, indexProperty.GetMethod, iterator); MethodInfo cloneMethod = CloneMethod.MakeGenericMethod(elementType); Expression clonedElement = ExpressionFactory.Call(context.CloneFactory, cloneMethod, getElementExpression); return ExpressionFactory.Call(context.TargetEntity, indexProperty.SetMethod, iterator, clonedElement); } }; }
private void CloneComplexProperty(IExpressionBuilder expressionBuilder, CloneExpressionContext parameters, PropertyInfo property) { MethodInfo cloneMethod = CloneMethod.MakeGenericMethod(property.PropertyType); Expression complexValue = ExpressionFactory.Call(parameters.SourceEntity, property.GetMethod); Expression subClone = ExpressionFactory.Call(parameters.CloneFactory, cloneMethod, complexValue); expressionBuilder.SetProperty( parameters.TargetEntity, property, subClone); }