Ejemplo n.º 1
0
            public Func <TSource, TDest> GetMapper(MappingParameters ps)
            {
                var em     = new ExpressionMapper <TS, TD>(ps);
                var mapper = em.GetMapper();

                return(source => (TDest)(object)mapper((TS)(object)source));
            }
Ejemplo n.º 2
0
            public Expression CheckNull(
                ExpressionMapper <TSource, TDest> mapper,
                Expression source,
                object nullValue,
                MapValue[]                      mapValues,
                object defaultValue,
                MapValue[]                      srcMapValues)
            {
                var param =
                    source.NodeType != ExpressionType.MemberAccess &&
                    source.NodeType != ExpressionType.Parameter &&
                    source.NodeType != ExpressionType.Constant?
                    Expression.Parameter(typeof(TS), "p") :
                    null;

                var nexpr = Expression.Constant(nullValue ?? default(TD));

                var expr =
                    source.NodeType == ExpressionType.Constant && ((ConstantExpression)source).Value == null ?
                    nexpr as Expression:
                    Expression.Condition(
                        Expression.Equal(param ?? source, Expression.Constant(null)),
                        nexpr.Value == null ? Expression.Convert(nexpr, typeof(TD)) : nexpr as Expression,
                        mapper.GetValueMapper(param ?? source, typeof(TD), false, null, mapValues, defaultValue, srcMapValues));

                return(param == null ? expr : Expression.Invoke(Expression.Lambda <Func <TS, TD> >(expr, param), source));
            }
Ejemplo n.º 3
0
            public Expression SourceMapValues(
                ExpressionMapper <TSource, TDest> mapper,
                Expression source,
                object nullValue,
                object defaultValue,
                MapValue[]                      srcMapValues)
            {
                var param =
                    //source.NodeType != ExpressionType.MemberAccess &&
                    source.NodeType != ExpressionType.Parameter &&
                    source.NodeType != ExpressionType.Constant?
                    Expression.Parameter(typeof(TS), "p") :
                    null;

                var expr = mapper.GetValueMapper(Expression.Constant(defaultValue), typeof(TD), true, nullValue, null, null, null);

                for (var i = srcMapValues.Length - 1; i >= 0; i--)
                {
                    var value = srcMapValues[i];

                    expr = Expression.Condition(
                        Expression.Equal(param ?? source, mapper.GetValueMapper(Expression.Constant(value.OrigValue), typeof(TS), false, null, null, null, null)),
                        mapper.GetValueMapper(Expression.Constant(value.MapValues[0]), typeof(TD), true, nullValue, null, null, null),
                        expr);
                }

                return(param == null ? expr : Expression.Invoke(Expression.Lambda <Func <TS, TD> >(expr, param), source));
            }
Ejemplo n.º 4
0
            public Expression MapLists(ExpressionMapper <TSource, TDest> mapper, Expression source)
            {
                var ts = TypeHelper.GetGenericType(typeof(IEnumerable <>), typeof(TS)).GetGenericArguments()[0];
                var td = TypeHelper.GetGenericType(typeof(IEnumerable <>), typeof(TD)).GetGenericArguments()[0];

                var type = typeof(ItemHelper <,>).MakeGenericType(typeof(TSource), typeof(TDest), typeof(TS), typeof(TD), ts, td);

                return(((IItemHelper)Activator.CreateInstance(type)).MapLists(mapper, source));
            }
Ejemplo n.º 5
0
            public Expression MapObjects(ExpressionMapper <TSource, TDest> mapper, Expression source)
            {
                var param = mapper._getCurrent == null ? (ParameterExpression)source : Expression.Parameter(source.Type, "source");

                Expression expr;
                object     m;

                if (mapper._parameters.MapperList.TryGetValue(new { S = typeof(TS), D = typeof(TD) }, out m))
                {
                    var map = (Mapper <TS, TD>)m;

                    if (map.Map == null)
                    {
                        expr = Expression.Invoke(
                            Expression.PropertyOrField(Expression.Constant(map), "Map"),
                            source, mapper._parameters.MappingContext);
                    }
                    else
                    {
                        expr = Expression.Invoke(Expression.Constant(map.Map), source, mapper._parameters.MappingContext);
                    }
                }
                else
                {
                    var exmap = new ExpressionMapper <TS, TD>(mapper._parameters);
                    expr = exmap.GetMemberInit(param);
                }

                if (mapper._getCurrent == null)
                {
                    return(expr);
                }

                if (!mapper.HandleBackReferences)
                {
                    Expression <Func <object> > func = () => MapObjects((TS)null, null);
                    return(Expression.Call((MethodInfo)ReflectionHelper.MemeberInfo(func), source, Expression.Lambda <Func <TS, TD> >(expr, param)));
                }
                else
                {
                    mapper._parameters.UseContext = true;

                    Expression <Func <object> > func = () => MapCrossReferences(null, null, null, null, null);

                    return(Expression.Call(
                               (MethodInfo)ReflectionHelper.MemeberInfo(func),
                               mapper._parameters.MappingContext,
                               source,
                               Expression.Lambda <Func <TS, TD> >(expr, param),
                               Expression.Constant(mapper._getCurrent),
                               Expression.Constant(mapper._setCurrent)));
                }
            }
Ejemplo n.º 6
0
            public Expression DestMapValues(
                ExpressionMapper <TSource, TDest> mapper,
                Expression source,
                object nullValue,
                MapValue[]                      mapValues,
                object defaultValue)
            {
                var param =
                    //source.NodeType != ExpressionType.MemberAccess &&
                    source.NodeType != ExpressionType.Parameter &&
                    source.NodeType != ExpressionType.Constant?
                    Expression.Parameter(typeof(TS), "p") :
                    null;

                var expr = mapper.GetValueMapper(Expression.Constant(defaultValue), typeof(TD), true, nullValue, null, null, null);

                for (var i = mapValues.Length - 1; i >= 0; i--)
                {
                    var value = mapValues[i];
                    var orex  = null as Expression;

                    foreach (var mapValue in value.MapValues)
                    {
                        var ex = Expression.Equal(param ?? source, mapper.GetValueMapper(Expression.Constant(mapValue), typeof(TS), false, null, null, null, null));
                        orex = orex == null ? ex : Expression.OrElse(orex, ex);
                    }

                    if (orex != null)
                    {
                        expr = Expression.Condition(
                            orex,
                            mapper.GetValueMapper(Expression.Constant(value.OrigValue), typeof(TD), true, nullValue, null, null, null),
                            expr);
                    }
                }

                return(param == null ? expr : Expression.Invoke(Expression.Lambda <Func <TS, TD> >(expr, param), source));
            }
Ejemplo n.º 7
0
                public Expression MapLists(ExpressionMapper <TSource, TDest> mapper, Expression source)
                {
                    var itemMapper =
                        new ExpressionMapper <TSourceItem, TDestItem>(mapper._parameters);

                    var itemParam = Expression.Parameter(typeof(TSourceItem), "item");
                    var itemExpr  = itemMapper.GetValueMapper(
                        itemParam,
                        typeof(TDestItem),
                        true,
                        mapper._parameters.MappingSchema.GetNullValue(typeof(TDestItem)),
                        mapper._parameters.MappingSchema.GetMapValues(typeof(TDestItem)),
                        mapper._parameters.MappingSchema.GetDefaultValue(typeof(TDestItem)),
                        mapper._parameters.MappingSchema.GetMapValues(typeof(TSourceItem)));

                    var itemLambda = Expression.Lambda <Func <TSourceItem, TDestItem> >(itemExpr, itemParam);

                    var isSourceScalar = !typeof(TSourceItem).IsArray && TypeHelper.IsScalar(typeof(TSourceItem));
                    var isDestScalar   = !typeof(TDestItem).IsArray && TypeHelper.IsScalar(typeof(TDestItem));

                    if (!mapper.HandleBackReferences || isSourceScalar || isDestScalar)
                    {
                        if (typeof(TD).IsArray)
                        {
                            Expression <Func <object> > arrMapper = () => MapScalarArray(null, null);
                            return(Expression.Call((MethodInfo)ReflectionHelper.MemeberInfo(arrMapper), source, itemLambda));
                        }

                        var isList =
                            typeof(TD) == typeof(IEnumerable <TDestItem>) || typeof(TD) == typeof(ICollection <TDestItem>) ||
                            typeof(TD) == typeof(IList <TDestItem>) || typeof(TD) == typeof(List <TDestItem>);

                        var method = typeof(ItemHelper <TSourceItem, TDestItem>).GetMethod("MapScalarList", BindingFlags.NonPublic | BindingFlags.Static);

                        method = method.MakeGenericMethod(isList ? typeof(List <TDestItem>) : typeof(TD));

                        return(Expression.Call(method, source, itemLambda));
                    }
                    else
                    {
                        mapper._parameters.UseContext = true;

                        var type = typeof(ClassItemHelper <,>).MakeGenericType(
                            typeof(TSource), typeof(TDest),
                            typeof(TS), typeof(TD),
                            typeof(TSourceItem), typeof(TDestItem));

                        var helper = ((IClassItemHelper)Activator.CreateInstance(type));

                        if (typeof(TD).IsArray)
                        {
                            return(Expression.Call(helper.GetObjectArrayInfo(), mapper._parameters.MappingContext, source, itemLambda));
                        }

                        var isList =
                            typeof(TD) == typeof(IEnumerable <TDestItem>) || typeof(TD) == typeof(ICollection <TDestItem>) ||
                            typeof(TD) == typeof(IList <TDestItem>) || typeof(TD) == typeof(List <TDestItem>);

                        return(Expression.Call(helper.GetObjectListInfo(isList), mapper._parameters.MappingContext, source, itemLambda));
                    }
                }