コード例 #1
0
        /// <summary>
        /// 创建公共成员
        /// </summary>
        protected void CreateCommonMember()
        {
            PropertyInfo[] propertiesSource = SourceType.GetProperties();
            foreach (PropertyInfo propSource in propertiesSource)
            {
                PropertyInfo propDest = TargetType.GetProperty(propSource.Name);
                if (propDest != null)
                {
                    // 检查是否已存在或被忽略。
                    bool ignorePropDest = _propertiesToIgnore.Exists(x => x.Name == propDest.Name) || PropertiesMapping.Exists(x => GetPropertyInfo(x.Item2).Name == propDest.Name);

                    if (propDest.CanWrite && !ignorePropDest)
                    {
                        Type sourceType = propSource.PropertyType;
                        Type destType = propDest.PropertyType;
                        bool isList = IsListOf(destType);
                        if (isList)
                        {
                            sourceType = TypeSystem.GetElementType(propSource.PropertyType);
                            destType = TypeSystem.GetElementType(propDest.PropertyType);
                        }

                        var canCreateConfig = CanCreateConfig(sourceType, destType);
                        if (canCreateConfig.CanCreate)
                        {
                            // 只创造现有的关系
                            Expression expSource = Expression.MakeMemberAccess(paramClassSource, propSource);
                            ParameterExpression paramDest = Expression.Parameter(TargetType, "t");
                            Expression expDest = Expression.MakeMemberAccess(paramDest, propDest);
                            PropertiesMapping.Add(Tuple.Create(expSource, expDest, false, canCreateConfig.MapperName));
                        }
                    }
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// 将表达式源的映射分配给属性目标。
 /// </summary>
 /// <param name="getPropertySource">属性源类型</param>
 /// <param name="getPropertyDest">属性目标类型</param>
 /// <param name="checkIfNull">是否检查null值</param>
 /// <param name="name">要使用的映射器的别名</param>
 internal MapperConfigurationBase ForMemberBase(Expression getPropertySource, Expression getPropertyDest, bool checkIfNull, string name = null)
 {
     // 添加到映射列表并且可以继续操作
     PropertiesMapping.Add(Tuple.Create(getPropertySource, getPropertyDest, checkIfNull, name));
     return this;
 }