Exemplo n.º 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);
        }
Exemplo n.º 2
0
 void IProfileConfiguration.Configure(TypeMapRegistry typeMapRegistry)
 {
     foreach (var typeMap in _typeMapConfigs.Select(config => typeMapRegistry.GetTypeMap(config.Types)))
     {
         Configure(typeMapRegistry, typeMap);
     }
 }
        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(ToObject(valueResolverExpr), Constant(null), DestinationType, ref parameterContext);
            }
            return valueResolverExpr;
        }
Exemplo n.º 4
0
        private void Configure(TypeMapRegistry typeMapRegistry, ITypeMapConfiguration typeMapConfiguration)
        {
            var typeMap = typeMapRegistry.GetTypeMap(typeMapConfiguration.Types);

            Configure(typeMapRegistry, typeMap);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
 private void Configure(TypeMapRegistry typeMapRegistry, ITypeMapConfiguration typeMapConfiguration)
 {
     var typeMap = typeMapRegistry.GetTypeMap(typeMapConfiguration.Types);
     Configure(typeMapRegistry, typeMap);
 }
Exemplo n.º 7
0
 void IProfileConfiguration.Configure(TypeMapRegistry typeMapRegistry)
 {
     foreach (var typeMap in _typeMapConfigs.Where(c => !c.IsOpenGeneric).Select(config => typeMapRegistry.GetTypeMap(config.Types)))
     {
         Configure(typeMapRegistry, typeMap);
     }
 }
Exemplo n.º 8
0
 public TypeMap FindTypeMapFor(TypePair typePair) => _typeMapRegistry.GetTypeMap(typePair);