C# 对象克隆,DataTable转LIST
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C# 对象克隆,DataTable转LIST,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3574字,纯文字阅读大概需要6分钟。
内容图文
![C# 对象克隆,DataTable转LIST](/upload/InfoBanner/zyjiaocheng/1123/32667d6b01e34f61a9b820232fb9ee3e.jpg)
public class ConvertHelper<T> where T : new() { privatestaticstring module = "ConvertHelper.cs"; publicstatic 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; } publicstatic 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>publicstatic 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; } }
原文:http://www.cnblogs.com/HelloXZ/p/3799737.html
内容总结
以上是互联网集市为您收集整理的C# 对象克隆,DataTable转LIST全部内容,希望文章能够帮你解决C# 对象克隆,DataTable转LIST所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。