Esempio n. 1
0
 private void CheckPrePostfixes(SourceMembers nameToMember, MemberInfo member)
 {
     foreach (var memberName in PossibleNames(member.Name, Config.Prefixes, Config.Postfixes))
     {
         nameToMember.TryAdd(memberName, member);
     }
 }
Esempio n. 2
0
        private SourceMembers PossibleNames()
        {
            var nameToMember = new SourceMembers(
                ReadAccessors.Length,
                StringComparer.OrdinalIgnoreCase
                );
            IEnumerable <MemberInfo> accessors = ReadAccessors;

            if (Config.MethodMappingEnabled)
            {
                accessors = AddMethods(accessors);
            }
            foreach (var member in accessors)
            {
                if (!nameToMember.ContainsKey(member.Name))
                {
                    nameToMember.Add(member.Name, member);
                }
                if (Config.Postfixes.Count == 0 && Config.Prefixes.Count == 0)
                {
                    continue;
                }
                CheckPrePostfixes(nameToMember, member);
            }
            return(nameToMember);
        }
Esempio n. 3
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);
        }
 private Expression ResolveSource(ParameterExpression sourceParameter, ParameterExpression contextParameter)
 {
     if (CustomExpression != null)
     {
         return(CustomExpression.ConvertReplaceParameters(sourceParameter).IfNotNull(DestinationType));
     }
     if (CustomValueResolver != null)
     {
         return(CustomValueResolver.ConvertReplaceParameters(sourceParameter, contextParameter));
     }
     if (Parameter.IsOptional)
     {
         DefaultValue = true;
         return(Constant(Parameter.GetDefaultValue(), Parameter.ParameterType));
     }
     return(SourceMembers.Aggregate(
                (Expression)sourceParameter,
                (inner, getter) => getter is MethodInfo
                         ? Call(getter.IsStatic() ? null : inner, (MethodInfo)getter)
                         : (Expression)MakeMemberAccess(getter.IsStatic() ? null : inner, getter)
                ).IfNotNull(DestinationType));
 }
Esempio 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);
        }