Ejemplo n.º 1
0
        /// <summary>
        /// 将源类型的属性值转换给目标类型同名的属性,排除要过滤的属性名称
        /// </summary>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <param name="filter">要过滤的属性名称</param>
        public void Cast(object source, object target, string[] filter)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            for (int i = 0; i < Properties.Count; i++)
            {
                CastProperty cp = Properties[i];

                if (cp.SourceProperty.Getter != null)
                {
                    object Value = cp.SourceProperty.Getter(source, null); //PropertyInfo.GetValue(source,null);
                    if (cp.TargetProperty.Setter != null)
                    {
                        if (filter == null)
                        {
                            cp.TargetProperty.Setter(target, Value, null);
                        }
                        else if (!filter.Contains(cp.TargetProperty.PropertyName))
                        {
                            cp.TargetProperty.Setter(target, Value, null);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 以两个要转换的类型作为构造函数,构造一个对应的转换类
        /// </summary>
        /// <param name="sourceType"></param>
        /// <param name="targetType"></param>
        public Mapping(Type sourceType, Type targetType)
        {
            PropertyInfo[] targetProperties = targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo sp in sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                foreach (PropertyInfo tp in targetProperties)
                {
                    if (sp.Name == tp.Name)
                    {
                        if (sp.PropertyType == tp.PropertyType)
                        {
                            CastProperty cp = new CastProperty();
                            cp.SourceProperty = new PropertyAccessorHandler(sp);
                            cp.TargetProperty = new PropertyAccessorHandler(tp);
                            Properties.Add(cp);
                            break;
                        }
                        else
                        {
                            //关于Nullable<T>跟T类型对比
                            //string nullableName = typeof (Nullable<>).Name;

                            Type spType = (typeof(Nullable <>).Name == sp.PropertyType.Name) ? sp.PropertyType.GetProperties()[1].PropertyType : sp.PropertyType;

                            Type tpType = (typeof(Nullable <>).Name == tp.PropertyType.Name) ? tp.PropertyType.GetProperties()[1].PropertyType : tp.PropertyType;

                            if (spType == tpType)
                            {
                                CastProperty cp = new CastProperty();
                                cp.SourceProperty = new PropertyAccessorHandler(sp);
                                cp.TargetProperty = new PropertyAccessorHandler(tp);
                                Properties.Add(cp);
                                break;
                            }
                        }
                    }
                }
            }
        }