from
static class extendclass { public static list<t> todatalist<t>(this datatable dt) { var list = new list<t>(); var plist = new list<propertyinfo>(typeof(t).getproperties()); foreach (datarow item in dt.rows) { t s = activator.createinstance<t>(); for (int i = 0; i < dt.columns.count; i++) { propertyinfo info = plist.find(p => p.name == dt.columns[i].columnname); if (info != null) { try { if (!convert.isdbnull(item[i])) { object v = null; if (info.propertytype.tostring().contains("system.nullable")) { v = convert.changetype(item[i], nullable.getunderlyingtype(info.propertytype)); } else { v = convert.changetype(item[i], info.propertytype); } info.setvalue(s, v, null); } } catch (exception ex) { throw new exception("字段[" + info.name + "]转换出错," + ex.message); } } } list.add(s); } return list; } }
如对本文有疑问, 点击进行留言回复!!
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
C#实现获取本地内网(局域网)和外网(公网)IP地址的方法分析
网友评论