private void CreatBindingFromSimple(ref Tuple <Expression, Expression, bool, string> configExpression, Type typeSource, Type typeTarget, PropertyInfo propTarget)
        {
            // 没有特殊的操作
            if (typeSource == typeTarget)
            {
                // 创建成员绑定
                CreateMemberBinding(configExpression.Item1, propTarget, configExpression.Item3);
            }
            else
            {
                // 尝试查找mapper
                MapperConfigurationBase externalMapper = GetAndCheckMapper(typeSource, typeTarget, configExpression.Item4);
                // 如果此时未初始化映射器
                externalMapper.CreateMappingExpression(_constructorFunc);
                // 默认情况下,检查对象的null
                Expression mapExpression     = externalMapper.GetMemberInitExpression();
                Expression defaultExpression = Expression.Constant(MapperHelper.GetDefaultValue(configExpression.Item1.Type), configExpression.Item1.Type);
                // 修改成员
                Expression expSource = visitorMapper.Visit(configExpression.Item1);
                ChangParameterExpressionVisitor changeParamaterVisitor = new ChangParameterExpressionVisitor(expSource);
                mapExpression = changeParamaterVisitor.Visit(mapExpression);
                // 现在可以创建正确的参数。
                Expression checkIfNull = Expression.NotEqual(expSource, defaultExpression);
                // 创建条件
                var checkExpression             = Expression.Condition(checkIfNull, mapExpression, Expression.Constant(MapperHelper.GetDefaultValue(mapExpression.Type), mapExpression.Type), mapExpression.Type);
                MemberAssignment bindExpression = Expression.Bind(propTarget, checkExpression);
                // 找到了映射器但没有配置
                if (string.IsNullOrEmpty(configExpression.Item4))
                {
                    configExpression = Tuple.Create(configExpression.Item1, configExpression.Item2, configExpression.Item3, externalMapper.Name);
                }

                memberForNew.Add(bindExpression);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 反向映射
        /// </summary>
        /// <param name="name">mapper别名</param>
        /// <returns>
        /// 新的mapper对象
        /// </returns>
        /// <exception cref="MapperExistException"></exception>
        public MapperConfiguration <TDest, TSource> ReverseMap(string name = null)
        {
            MapperConfigurationBase map = GetMapper(typeof(TDest), typeof(TSource), false, name);

            if (map != null)
            {
                throw new MapperExistException(typeof(TDest), typeof(TSource));
            }
            string finalName = string.IsNullOrEmpty(name) ? "s" + (MapperConfigurationCollectionContainer.Instance.Count).ToString() : name;

            map = new MapperConfiguration <TDest, TSource>(finalName);
            MapperConfigurationCollectionContainer.Instance.Add(map);
            CreateCommonMember();

            // 现有属性的映射,并且创建反向关系
            foreach (var item in PropertiesMapping)
            {
                PropertyInfo propertyDest = GetPropertyInfo(item.Item1);
                if (propertyDest.CanWrite)
                {
                    if (!string.IsNullOrEmpty(item.Item4))
                    {
                        //找到反向关系的mapper
                        var reverseMapper = GetMapper(item.Item2.Type, item.Item1.Type, false);
                        if (reverseMapper != null)
                        {
                            map.ForMemberBase(item.Item2, item.Item1, item.Item3, reverseMapper.Name);
                        }
                    }
                    else
                    {
                        if (item.Item1.NodeType == ExpressionType.MemberAccess)
                        {
                            map.ForMemberBase(item.Item2, item.Item1, item.Item3, item.Item4);
                        }
                    }
                }
            }

            return((MapperConfiguration <TDest, TSource>)map);
        }