• C# 对象克隆,DataTable转LIST


    public class ConvertHelper<T> where T : new()
        {
            private static string module = "ConvertHelper.cs";
    
            public static ObservableCollection<T> ConvertToList(List<T> listobject)
            {
                ObservableCollection<T> collection = null;
                try
                {
                    collection = new ObservableCollection<T>(listobject);
                }
                catch (Exception ex)
                {
                    ServiceLocator.Current.GetInstance<IWriteLog>().Log(LogConstant.LogType.Exception, module,
                       "Error occurs on ConvertToList modules: {0}.", ex.Message);
                }
    
                return collection;
            }
    
            public static ObservableCollection<T> ConvertToObservable(DataTable dt)
            {
                ObservableCollection<T> collection = null;
    
                // 定义集合
                List<T> ts = new List<T>();
    
                try
                {
                    // 获得此模型的类型
                    Type type = typeof(T);
    
                    // 定义一个临时变量
                    string tempName = string.Empty;
    
                    // 遍历DataTable中所有的数据行
                    foreach (DataRow dr in dt.Rows)
                    {
                        T t = new T();
    
                        // 获得此模型的公共属性
                        PropertyInfo[] propertys = t.GetType().GetProperties();
    
                        // 遍历该对象的所有属性
                        foreach (PropertyInfo pi in propertys)
                        {
                            // 将属性名称赋值给临时变量
                            tempName = pi.Name;
    
                            // 检查DataTable是否包含此列(列名==对象的属性名)  
                            if (dt.Columns.Contains(tempName))
                            {
                                // 判断此属性是否有Setter 该属性不可写,直接跳出
                                if (!pi.CanWrite) continue;
    
                                // 取值
                                object value = dr[tempName];
    
                                // 如果非空,则赋给对象的属性
                                if (value != DBNull.Value)
                                    pi.SetValue(t, value.ToString(), null);
                            }
                        }
    
                        // 对象添加到泛型集合中
                        ts.Add(t);
                    }
                    collection = new ObservableCollection<T>(ts);
                }
                catch (Exception ex)
                {
                    ServiceLocator.Current.GetInstance<IWriteLog>().Log(LogConstant.LogType.Exception, module,
                      "Error occurs on ConvertToList modules: {0}.", ex.Message);
                }
    
                return collection;
            }
    
            /// 利用反射和泛型
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static List<T> ConvertToList(DataTable dt)
            {
                // 定义集合
                List<T> ts = new List<T>();
    
                try
                {
                    // 获得此模型的类型
                    Type type = typeof(T);
    
                    // 定义一个临时变量
                    string tempName = string.Empty;
    
                    // 遍历DataTable中所有的数据行
                    foreach (DataRow dr in dt.Rows)
                    {
                        T t = new T();
    
                        // 获得此模型的公共属性
                        PropertyInfo[] propertys = t.GetType().GetProperties();
    
                        // 遍历该对象的所有属性
                        foreach (PropertyInfo pi in propertys)
                        {
                            // 将属性名称赋值给临时变量
                            tempName = pi.Name;
    
                            // 检查DataTable是否包含此列(列名==对象的属性名)  
                            if (dt.Columns.Contains(tempName))
                            {
                                // 判断此属性是否有Setter 该属性不可写,直接跳出
                                if (!pi.CanWrite) continue;
    
                                // 取值
                                object value = dr[tempName];
    
                                // 如果非空,则赋给对象的属性
                                if (value != DBNull.Value)
                                    pi.SetValue(t, value.ToString(), null);
                            }
                        }
    
                        // 对象添加到泛型集合中
                        ts.Add(t);
                    }
                }
                catch (Exception ex)
                {
                    ServiceLocator.Current.GetInstance<IWriteLog>().Log(LogConstant.LogType.Exception, module,
                      "Error occurs on ConvertToList modules: {0}.", ex.Message);
                }
    
                return ts;
            }
        }

    对象克隆赋值

     /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                List<Test> list = new List<Test>();
                Test test = new Test();
               
                test.ID = "1";
                test.NAME = "xz";
                list.Add(test);
                Test test1=new Test ();
                CopyValue(list[0], test1);
    
                test1.NAME = "xznihoa";
                list.Add(test1);
            }
    
            public static void CopyValue(object origin, object target)
            {
                System.Reflection.PropertyInfo[] properties = (target.GetType()).GetProperties();
                System.Reflection.PropertyInfo[]   fields = (origin.GetType()).GetProperties();
                for (int i = 0; i < fields.Length; i++)
                {
                    for (int j = 0; j < properties.Length; j++)
                    {
                        if (fields[i].Name == properties[j].Name && properties[j].CanWrite)
                        {
                            properties[j].SetValue(target, fields[i].GetValue(origin,null), null);
                        }
                    }
                }
            }
    
        }
    
        public class Test
        {
            public string ID { get; set; }
    
            public string NAME { get; set; }
        }
    

      

  • 相关阅读:
    ASP在线群发源码!Jmail的需先安装jmail组件!
    用JS动态改变表单form里的action值属性的方法
    一些SqlBuckCopy心得
    近期关于Thread使用的一些感想.
    焦点图切换实现
    评国内三大B2C网站首页的信息架构
    20个改变网站用户体验的方法
    从信任出发建立电商用户体验体系
    Dom操作之兼容技巧
    9个移动网站优化的最佳实践
  • 原文地址:https://www.cnblogs.com/HelloXZ/p/3799737.html
Copyright © 2020-2023  润新知