public static ObservableCollection<F> Transform<T, F>(List<T> target)
                      where F : new()
        {
            ObservableCollection<F> source = new ObservableCollection<F>();
            ; 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>();
            ; 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>();
            ; 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>();
            ; 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;
            }

            ; f < fromFields.Length; f++)
            {
                fromField = (PropertyInfo)fromFields[f];

                ; 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 - , , ).Ticks) / ,
                                    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;

                }
            }
        }

AutoTransformHandler的更多相关文章

随机推荐

  1. Python魔术方法-Magic Method

    介绍 在Python中,所有以"__"双下划线包起来的方法,都统称为"Magic Method",例如类的初始化方法 __init__ ,Python中所有的魔 ...

  2. Nginx反向代理配置可跨域

    由于业务需要,同一项目中的前端代码放在静态环境中,而后端代码放在tomcat中,但此时问题却出现了:前端使用ajax请求后端获取数据时出现如下报错 XMLHttpRequest cannot load ...

  3. python 面向对象和类成员和异常处理

    python 面向对象 你把自己想象成一个上帝,你要创造一个星球,首先你要把它揉成一个个球,两个直径就能创造一个球 class star: '''名字(name),赤道直径(equatorial di ...

  4. JavaScript 回忆录

    作者:禅楼望月(http://www.cnblogs.com/yaoyinglong) 1. 基本类型和应用类型 1.1 复制变量值的不同 值类型的变量在复制变量值后互不影响,因为值类型本身保存的就是 ...

  5. iOS 获取User-Agent

    第一种方法 UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];    NSString *userAgent = [w ...

  6. 自定义View绘制字符串

    import android.app.Activity; import android.os.Bundle; import android.view.Display; import android.v ...

  7. 个人推荐,比较好的MySQL客户端工具

    关于啊,MySQL客户端,实在是太多了.本人也是正在摸索中. MySQL Workbench phpMyAdmin SQLyog Navicat for MySQL MySQL Server MySQ ...

  8. javascript 的Date 格式化, 模仿shell中date命令的格式

    原文:javascript 的Date 格式化, 模仿shell中date命令的格式 shell 中显示当前的日期 [root@localhost]$ date '+%Y-%m-%d %H:%M:%S ...

  9. UIButton根据不同的状态设置layer

    这需要用到KVO,监听button的highlighted属性的变化,在监听回调里根据监听到得属性值设置layer 设置监听如下 [button  addObserver:self forKeyPat ...

  10. Android:百度地图 + 百度导航

    地图SDK 开发指南:http://lbsyun.baidu.com/index.php?title=androidsdk/guide/introduction 导航SDK开发指南:http://lb ...