Exemplo n.º 1
0
        private static Expression?DictionaryFn(Expression source, IMemberModel destinationMember, CompileArgument arg)
        {
            var dictType = source.Type.GetDictionaryType();

            if (dictType == null)
            {
                return(null);
            }

            var strategy = arg.Settings.NameMatchingStrategy;
            var destinationMemberName = destinationMember.GetMemberName(arg.Settings.GetMemberNames, strategy.DestinationMemberNameConverter);
            var key  = Expression.Constant(destinationMemberName);
            var args = dictType.GetGenericArguments();

            if (strategy.SourceMemberNameConverter != NameMatchingStrategy.Identity)
            {
                var method = typeof(MapsterHelper).GetMethods().First(m => m.Name == nameof(MapsterHelper.FlexibleGet)).MakeGenericMethod(args[1]);
                return(Expression.Call(method, source.To(dictType), key, MapsterHelper.GetConverterExpression(strategy.SourceMemberNameConverter)));
            }
            else
            {
                var method = typeof(MapsterHelper).GetMethods().First(m => m.Name == nameof(MapsterHelper.GetValueOrDefault)).MakeGenericMethod(args);
                return(Expression.Call(method, source.To(dictType), key));
            }
        }
Exemplo n.º 2
0
        private static Expression?FlattenMemberFn(Expression source, IMemberModel destinationMember, CompileArgument arg)
        {
            var strategy = arg.Settings.NameMatchingStrategy;
            var destinationMemberName = destinationMember.GetMemberName(arg.Settings.GetMemberNames, strategy.DestinationMemberNameConverter);

            return(GetDeepFlattening(source, destinationMemberName, arg));
        }
Exemplo n.º 3
0
        internal static IEnumerable <InvokerModel> FindUnflatteningPairs(Expression source, IMemberModel destinationMember, CompileArgument arg)
        {
            var strategy = arg.Settings.NameMatchingStrategy;
            var destinationMemberName = destinationMember.GetMemberName(arg.Settings.GetMemberNames, strategy.DestinationMemberNameConverter);
            var members = source.Type.GetFieldsAndProperties(accessorFlags: BindingFlags.NonPublic | BindingFlags.Public);

            foreach (var member in members)
            {
                if (!member.ShouldMapMember(arg, MemberSide.Source))
                {
                    continue;
                }
                var sourceMemberName = member.GetMemberName(arg.Settings.GetMemberNames, strategy.SourceMemberNameConverter);
                if (!sourceMemberName.StartsWith(destinationMemberName) || sourceMemberName == destinationMemberName)
                {
                    continue;
                }
                foreach (var prop in GetDeepUnflattening(destinationMember, sourceMemberName.Substring(destinationMemberName.Length).TrimStart('_'), arg))
                {
                    yield return(new InvokerModel
                    {
                        SourceMemberName = member.Name,
                        DestinationMemberName = destinationMember.Name + "." + prop,
                    });
                }
            }
        }
Exemplo n.º 4
0
        private static Expression?PropertyOrFieldFn(Expression source, IMemberModel destinationMember, CompileArgument arg)
        {
            var members  = source.Type.GetFieldsAndProperties(accessorFlags: BindingFlags.NonPublic | BindingFlags.Public);
            var strategy = arg.Settings.NameMatchingStrategy;
            var destinationMemberName = destinationMember.GetMemberName(arg.Settings.GetMemberNames, strategy.DestinationMemberNameConverter);

            return(members
                   .Where(member => member.ShouldMapMember(arg, MemberSide.Source))
                   .Where(member => member.GetMemberName(arg.Settings.GetMemberNames, strategy.SourceMemberNameConverter) == destinationMemberName)
                   .Select(member => member.GetExpression(source))
                   .FirstOrDefault());
        }
Exemplo n.º 5
0
        private static Expression?GetMethodFn(Expression source, IMemberModel destinationMember, CompileArgument arg)
        {
            if (arg.MapType == MapType.Projection)
            {
                return(null);
            }
            var strategy = arg.Settings.NameMatchingStrategy;
            var destinationMemberName = "Get" + destinationMember.GetMemberName(MemberSide.Destination, arg.Settings.GetMemberNames, strategy.DestinationMemberNameConverter);
            var getMethod             = Array.Find(source.Type.GetMethods(BindingFlags.Public | BindingFlags.Instance), m => strategy.SourceMemberNameConverter(m.Name) == destinationMemberName && m.GetParameters().Length == 0);

            if (getMethod == null)
            {
                return(null);
            }
            if (getMethod.Name == "GetType" && destinationMember.Type != typeof(Type))
            {
                return(null);
            }
            return(Expression.Call(source, getMethod));
        }