예제 #1
0
        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);
                       }
            };
        }
예제 #2
0
        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);
                       }
            };
        }
예제 #3
0
 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));
 }
예제 #4
0
        private void ClonePrimitiveProperty(IExpressionBuilder expressionBuilder, CloneExpressionContext parameters, PropertyInfo property)
        {
            Expression sourceValue = ExpressionFactory.Call(parameters.SourceEntity, property.GetMethod);

            expressionBuilder.SetProperty(
                parameters.TargetEntity,
                property,
                sourceValue);
        }
예제 #5
0
        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);
        }
예제 #6
0
        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);
        }
예제 #7
0
        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));
        }