C# 객체 클론, DataTable - LIST

12654 단어 Datatable
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; }

    }


좋은 웹페이지 즐겨찾기