C# 对象克隆,DataTable转LIST
2024-09-08 10:05:20
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; }
}
最新文章
- D3中动画(transition函数)的使用
- GJM : Python简单爬虫入门 (一) [转载]
- bzoj1211: prufer序列 | [HNOI2004]树的计数
- Linux下利用CGroup控制CPU、内存以及IO的操作记录
- Redis主从配置详细过程
- Delphi遍历文件夹
- hdu 1575 Tr A(矩阵快速幂,简单)
- C# DataGridView 导出 Excel(根据Excel版本显示选择不同后缀格式xls或xlsx)
- COJ 0501 取数游戏(TPM)
- [原创作品]Javascript内存管理机制
- window.open() | close()方法
- 深入 理解 Statement 和 PreparedStatement
- 正则表达式(c#)
- redis的常用公共方法
- 遍历JSON
- IP地址在mysql的存储(IP地址和int的转换)
- boot sector FAT
- 【NOI】荷马史诗
- IAR使用notice
- sshpass 指定密码远程 ssh 到服务器 或者 scp 发送文件到服务器
热门文章
- zabbix_agent自动发现服务端口
- 笔记,js对象浅析
- Vova and Trophies CodeForces - 1082B(思维题)
- Sending Secret Messages LightOJ - 1404
- LNOI2019 退役记
- render: h =>; h(App) $mount 什么意思
- TensorFlow 学习笔记(2)----placeholder的使用
- Maven学习总结(27)——Maven自定义打包插件maven-assembly-plugin详解
- CF410div2 D. Mike and distribution
- App架构设计经验谈:接口”安全机制”的设计