C# 两个类的映射

使用对象映射的方式来赋值,实际上是利用了反射的机制,达到动态生成对象和动态类型转换的目的。
1.映射转换 从T1转换到T2 则根据T2的属性取T1对应属性值

        /// 
        /// 映射转换 从T1转换到T2  则根据T2的属性取T1对应属性值
        /// 
        /// 转换前的对象
        /// 转换后的对象
        /// 对象数据
        /// 回调函数 列如查出的是id  需要显示name情况
        /// 
        public static T2 TransferData(T1 fromModel,Action action=null) where T2 : class
        {
            if (fromModel == null)
                return null;
            var t2 = IOC.CreateInstance();
            PropertyInfo[] propertys1 = IOC.GetProperties(typeof(T1));
            PropertyInfo[] propertys2 = IOC.GetProperties(typeof(T2));
            propertys2.ToList().ForEach(x => 
            {
                var p1 = propertys1.FirstOrDefault(o => o.Name == x.Name);
                if (p1 != null && x.CanWrite)
                {
                    try
                    {
                        var value = p1.GetValue(fromModel);
                        if (x.PropertyType == typeof(string))
                        {
                            x.SetValue(t2, value?.ToString());
                        }
                        else if (x.PropertyType == typeof(DateTime))
                        {
                            var datetime = Convert.ToDateTime(value);
                            x.SetValue(t2, datetime);
                        }
                        else if (x.PropertyType == typeof(int))
                        {
                            x.SetValue(t2, value.ChangeType(0));
                        }
                        else if (x.PropertyType == typeof(long))
                        {
                            x.SetValue(t2, value.ChangeType(0));
                        }
                        else if (x.PropertyType == typeof(decimal))
                        {
                            x.SetValue(t2, value.ChangeType(0));
                        }
                        else if (x.PropertyType == typeof(double))
                        {
                            x.SetValue(t2, value.ChangeType(0));
                        }
                        else if (x.PropertyType == typeof(float))
                        {
                            x.SetValue(t2, value.ChangeType(0));
                        }
                        else if(x.CanWrite)
                        {
                            x.SetValue(t2, value);
                        }
                    }
                    catch (Exception e)
                    {
            IO.Log.WriteLog(e.Message+ ",StackTrace:" + e.StackTrace);
                    }
                }
            });
            if (action != null)
            {
                action(fromModel,t2);
            }
            return t2;
        }

2.合并两个对象 从T1往T2合并 如果T2里面的数据是默认类型值 则从T1取数据往T2填充

        /// 
        /// 合并两个对象 从T1往T2合并  如果T2里面的数据是默认类型值  则从T1取数据往T2填充
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T2 MergeData(T1 fromModel,T2 afterModel, Action action = null) where T2 : class
        {
            if (fromModel == null)
                return afterModel;
            if (afterModel == null)
                return null;
            //var t2 = IOC.CreateInstance();
            PropertyInfo[] propertys1 = IOC.GetProperties(typeof(T1));
            PropertyInfo[] propertys2 = IOC.GetProperties(typeof(T2));
            propertys2.ToList().ForEach(x =>
            {
                var p1 = propertys1.FirstOrDefault(o => o.Name == x.Name);
                var p2Value = x.GetValue(afterModel);
                if (p1 != null)
                {
                    try
                    {
                        var p1Value = p1.GetValue(fromModel);

                        if (x.PropertyType == typeof (string))
                        {
                            if (p1Value != null && p1Value.ToString() != "")
                                x.SetValue(afterModel, p1Value.ToString());
                        }
                        else if (x.PropertyType == typeof (DateTime))
                        {
                            //var datetime = Convert.ToDateTime(value);
                            //x.SetValue(t2, datetime);
                        }
                        else if (x.PropertyType == typeof (int))
                        {
                            if (Convert.ToInt32(p2Value) == 0 && Convert.ToInt32(p1Value) != 0)
                                x.SetValue(afterModel, p1Value);
                        }
                        else if (x.PropertyType == typeof (decimal))
                        {
                            if (Convert.ToDecimal(p2Value) == 0 && Convert.ToDecimal(p1Value) != 0)
                                x.SetValue(afterModel, p1Value);
                        }
                        else if (p2Value == null && p1Value != null)
                        {
                            x.SetValue(afterModel, p1Value);
                        }
                    }
                    catch
                    {
                        //ex.Error();
                    }
                }
            });
            if (action != null)
            {
                action(fromModel, afterModel);
            }
            return afterModel;
        }

3.转换一个集合类型数据

        /// 
        /// 转换一个集合类型数据
        /// 
        /// 
        /// 
        /// 数据源
        /// 回调函数 列如查出的是id  需要显示name情况
        /// 
        public static List TransferData(IEnumerable list,Action action = null) where T2 : class
        {
            if (list == null)
                return null;
            var result = new List();
            foreach(var item in list)
            {
                var model = TransferData(item,action);
                result.Add(model);
            }
            return result;
        }

你可能感兴趣的:(C# 两个类的映射)