Exemple #1
0
        public Expression CreateExpression(TypeMapRegistry typeMapRegistry,
                                           ParameterExpression srcParam,
                                           ParameterExpression ctxtParam,
                                           ref ParameterExpression parameterContext)
        {
            if (CustomExpression != null)
            {
                return(CustomExpression.ConvertReplaceParameters(srcParam).IfNotNull());
            }

            if (CustomValueResolver != null)
            {
                return(Invoke(Constant(CustomValueResolver), srcParam, ctxtParam));
            }

            if (!SourceMembers.Any() && Parameter.IsOptional)
            {
                return(Constant(Parameter.GetDefaultValue()));
            }

            if (typeMapRegistry.GetTypeMap(new TypePair(SourceType, DestinationType)) == null &&
                Parameter.IsOptional)
            {
                return(Constant(Parameter.GetDefaultValue()));
            }

            var valueResolverExpr = SourceMembers.Aggregate(
                (Expression)srcParam,
                (inner, getter) => getter.MemberInfo is MethodInfo
                    ? getter.MemberInfo.IsStatic()
                        ? Call(null, (MethodInfo)getter.MemberInfo, inner)
                        : (Expression)Call(inner, (MethodInfo)getter.MemberInfo)
                    : MakeMemberAccess(getter.MemberInfo.IsStatic() ? null : inner, getter.MemberInfo)
                );

            valueResolverExpr = valueResolverExpr.IfNotNull();

            if ((SourceType.IsEnumerableType() && SourceType != typeof(string)) ||
                typeMapRegistry.GetTypeMap(new TypePair(SourceType, DestinationType)) != null ||
                !DestinationType.IsAssignableFrom(SourceType))
            {
                /*
                 * var value = context.Mapper.Map(result, null, sourceType, destinationType, context);
                 */
                return(TypeMapPlanBuilder.ContextMap(valueResolverExpr, Default(DestinationType), DestinationType, ref parameterContext));
            }
            return(valueResolverExpr);
        }
Exemple #2
0
        public Expression CreateExpression(TypeMapRegistry typeMapRegistry,
                                           ParameterExpression srcParam,
                                           ParameterExpression ctxtParam)
        {
            if (CustomExpression != null)
            {
                return(CustomExpression.ConvertReplaceParameters(srcParam).IfNotNull());
            }

            if (CustomValueResolver != null)
            {
                return(Invoke(Constant(CustomValueResolver), srcParam, ctxtParam));
            }

            if (!SourceMembers.Any() && Parameter.IsOptional)
            {
                return(Constant(Parameter.GetDefaultValue()));
            }

            if (typeMapRegistry.GetTypeMap(new TypePair(SourceType, DestinationType)) == null &&
                Parameter.IsOptional)
            {
                return(Constant(Parameter.GetDefaultValue()));
            }

            var valueResolverExpr = SourceMembers.Aggregate(
                (Expression)srcParam,
                (inner, getter) => getter.MemberInfo is MethodInfo
                    ? getter.MemberInfo.IsStatic()
                        ? Call(null, (MethodInfo)getter.MemberInfo, inner)
                        : (Expression)Call(inner, (MethodInfo)getter.MemberInfo)
                    : MakeMemberAccess(getter.MemberInfo.IsStatic() ? null : inner, getter.MemberInfo)
                );

            valueResolverExpr = valueResolverExpr.IfNotNull();

            if ((SourceType.IsEnumerableType() && SourceType != typeof(string)) ||
                typeMapRegistry.GetTypeMap(new TypePair(SourceType, DestinationType)) != null ||
                !DestinationType.IsAssignableFrom(SourceType))
            {
                /*
                 * var value = context.Mapper.Map(result, null, sourceType, destinationType, context);
                 */

                var mapperProp = MakeMemberAccess(ctxtParam, typeof(ResolutionContext).GetProperty("Mapper"));
                var mapMethod  = typeof(IRuntimeMapper).GetMethod("Map",
                                                                  new[] { typeof(object), typeof(object), typeof(Type), typeof(Type), typeof(ResolutionContext) });
                valueResolverExpr = Call(
                    mapperProp,
                    mapMethod,
                    ToObject(valueResolverExpr),
                    Constant(null),
                    Constant(SourceType),
                    Constant(DestinationType),
                    ctxtParam
                    );
            }


            return(valueResolverExpr);
        }