• AutoTransformHandler


    public static ObservableCollection<F> Transform<T, F>(List<T> target)
                          where F : new()
            {
                ObservableCollection<F> source = new ObservableCollection<F>();
                for (int i = 0; i < target.Count; i++)
                {
                    F f = new F();
                    f = Transform<T, F>(target[i]);
                    source.Add(f);
                }
                return source;
            }
    
            public static List<F> Transform<T, F>(ObservableCollection<T> target)
                            where F : new()
            {
                List<F> source = new List<F>();
                for (int i = 0; i < target.Count; i++)
                {
                    F f = new F();
                    f = Transform<T, F>(target[i]);
                    source.Add(f);
                }
                return source;
            }
    
            public static List<F> TransformList<T, F>(List<T> target)
                    where F : new()
            {
                List<F> source = new List<F>();
                for (int i = 0; i < target.Count; i++)
                {
                    F f = new F();
                    f = Transform<T, F>(target[i]);
                    source.Add(f);
                }
                return source;
            }
    
            public static ObservableCollection<F> TransformObservableCollection<T, F>(ObservableCollection<T> target)
                    where F : new()
            {
                ObservableCollection<F> source = new ObservableCollection<F>();
                for (int i = 0; i < target.Count; i++)
                {
                    F f = new F();
                    f = Transform<T, F>(target[i]);
                    source.Add(f);
                }
                return source;
            }
    
            public static F Transform<T, F>(T target)
                             where F : new()
            {
    
                if (target == null)
                {
                    return default(F);
                }
                F source = new F();
                if (target != null)
                {
                    PropertyInfo[] fromFields = null;
                    PropertyInfo[] toFields = null;
    
                    fromFields = typeof(T).GetProperties();
                    toFields = typeof(F).GetProperties();
    
                    SetProperties(fromFields, toFields, target, source);
                }
                return source;
            }
    
            public static void Transform<T, F>(T target, F source)
            {
                PropertyInfo[] fromFields = null;
                PropertyInfo[] toFields = null;
    
                fromFields = typeof(T).GetProperties();
                toFields = typeof(F).GetProperties();
    
                SetProperties(fromFields, toFields, target, source);
            }
    
            public static void SetProperties(PropertyInfo[] fromFields,
                                              PropertyInfo[] toFields,
                                              object fromRecord,
                                              object toRecord)
            {
                PropertyInfo fromField = null;
                PropertyInfo toField = null;
    
    
    
                if (fromFields == null)
                {
                    return;
                }
                if (toFields == null)
                {
                    return;
                }
    
                for (int f = 0; f < fromFields.Length; f++)
                {
                    fromField = (PropertyInfo)fromFields[f];
    
                    for (int t = 0; t < toFields.Length; t++)
                    {
    
                        toField = (PropertyInfo)toFields[t];
    
                        if (fromField.Name.ToUpper() != toField.Name.ToUpper())
                        {
                            continue;
                        }
    
                        if (toField.CanWrite)
                        {
    
                            if ((fromField.PropertyType == typeof(DateTime?) &&
                                 toField.PropertyType == typeof(string)))
                            {
                                DateTime? fromDateTime = (DateTime?)fromField.GetValue(fromRecord, null);
                                if (!fromDateTime.HasValue)
                                {
                                    toField.SetValue(toRecord,
                                    null,
                                    null);
                                }
                                else
                                {
                                    toField.SetValue(toRecord,
                                    fromDateTime.Value.ToString(),
                                    null);
                                }
                                break;
                            }
    
                            if (((fromField.PropertyType == typeof(DateTime)
                                || fromField.PropertyType == typeof(Int32)
                                || fromField.PropertyType == typeof(decimal?)
                                ) &&
                                toField.PropertyType == typeof(string)))
                            {
                                object fromDateTime = fromField.GetValue(fromRecord, null);
                                toField.SetValue(toRecord,
                                fromDateTime.ToString(),
                                null);
                                break;
                            }
    
                            if ((fromField.PropertyType == typeof(DateTime?) &&
                                    toField.PropertyType == typeof(long?)))
                            {
                                DateTime? fromDateTime = (DateTime?)fromField.GetValue(fromRecord, null);
                                if (!fromDateTime.HasValue)
                                {
                                    toField.SetValue(toRecord,
                                    null,
                                    null);
                                }
                                else
                                {
                                    toField.SetValue(toRecord,
                                        (((DateTime)fromDateTime.Value).Ticks - new DateTime(1970, 1, 1).Ticks) / 10000000,
                                        null);
                                }
                                break;
                            }
                            if (fromField.PropertyType == typeof(decimal?)
                                    && toField.PropertyType == typeof(double?))
                            {
                                double? toDouble = null;
                                if (fromField.GetValue(fromRecord, null) != null)
                                {
                                    toDouble = double.Parse(fromField.GetValue(fromRecord, null).ToString());
                                }
                                toField.SetValue(toRecord, toDouble, null);
                                break;
                            }
    
                            if (fromField.PropertyType == typeof(bool?)
                                   && toField.PropertyType == typeof(string))
                            {
                                string toString = null;
                                if (fromField.GetValue(fromRecord, null) != null)
                                {
                                    toString = fromField.GetValue(fromRecord, null).ToString();
                                }
                                toField.SetValue(toRecord, toString, null);
                                break;
                            }
    
                            toField.SetValue(toRecord,
                                             fromField.GetValue(fromRecord, null),
                                             null);
                            break;
    
                        }
                        toField.SetValue(toRecord,
                                         fromField.GetValue(fromRecord, null),
                                         null);
                        break;
    
                    }
                }
            }
  • 相关阅读:
    UVa 12716 GCD XOR (简单证明)
    2.12 运行计划并取得数据行
    nyoj 628 小媛在努力 【搜索】
    ArcGIS Server 10.2 公布Oracle11g数据源的 Feature Service
    项目复习期总结3:CSS引入方式,凝视,命名规范,背景,行高,文本属性
    Android使用有道翻译API实如今线翻译功能
    _00017 Kafka的体系结构介绍以及Kafka入门案例(0基础案例+Java API的使用)
    夜&#183; 启程
    你不知道的JavaScript(六)Box&Unbox
    pugixml读取unicode编码的xml文件的做法
  • 原文地址:https://www.cnblogs.com/Wolfmanlq/p/4183419.html
Copyright © 2020-2023  润新知