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 AddArrayExpressions <TEntity>(IExpressionBuilder expressionBuilder, CloneExpressionContext context) { context.TargetEntity = expressionBuilder.CreateObject <TEntity>( nameof(CloneExpressionContext.TargetEntity), new[] { typeof(int) }, new[] { ExpressionFactory.Call(context.SourceEntity, ArrayLengthProperty.GetMethod) }); expressionBuilder.For(context.SourceEntity, CloneArrayElementDelegate(context)); }
private void ClonePrimitiveProperty(IExpressionBuilder expressionBuilder, CloneExpressionContext parameters, PropertyInfo property) { Expression sourceValue = ExpressionFactory.Call(parameters.SourceEntity, property.GetMethod); expressionBuilder.SetProperty( parameters.TargetEntity, property, sourceValue); }
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); }
public void CallIsExpected() { Expression actual = TestObject.Call(TestParameterOne, TestMethod); Assert.IsInstanceOfType(actual, typeof(MethodCallExpression)); MethodCallExpression actualExpression = (MethodCallExpression)actual; Assert.AreEqual(TestParameterOne, actualExpression.Object); Assert.AreEqual(TestMethod, actualExpression.Method); Assert.AreEqual(0, actualExpression.Arguments.Count); }
private void AddListExpressions <TEntity>(IExpressionBuilder expressionBuilder, CloneExpressionContext context) { PropertyInfo listCountProperty = context .SourceEntity .Type .GetProperty(nameof(List <TEntity> .Count)); context.TargetEntity = expressionBuilder.CreateObject <TEntity>( nameof(CloneExpressionContext.TargetEntity), new[] { typeof(int) }, new[] { ExpressionFactory.Call(context.SourceEntity, listCountProperty.GetMethod) }); expressionBuilder.For(context.SourceEntity, CloneListElementDelegate(expressionBuilder, context)); }