博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ToList和ToDataTable(其中也有反射的知识)
阅读量:6756 次
发布时间:2019-06-26

本文共 3286 字,大约阅读时间需要 10 分钟。

using System;

using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication13

{
public static class ListAndTableExtension
{
public static DataTable ToDataTable<T>(this List<T> list) where T : new()
{
DataTable table = new DataTable();
PropertyInfo[] ps = typeof(T).GetProperties();
foreach (PropertyInfo p in ps)
{
if (!p.PropertyType.IsGenericType)
{
table.Columns.Add(p.Name, p.PropertyType);

}

else
{
Type GenericTypeDefinition= p.PropertyType.GetGenericTypeDefinition();
if (GenericTypeDefinition == typeof(Nullable<>))
{
table.Columns.Add(p.Name,Nullable.GetUnderlyingType(p.PropertyType));
}
}
}
foreach (T obj in list)
{
DataRow row = table.NewRow();
foreach (PropertyInfo p in ps)
{
row[p.Name] = p.GetValue(obj);
}
table.Rows.Add(row);
}
return table;
}
public static List<T> ToList<T>(this DataTable table) where T:new()
{
List<T> list = new List<T>();
PropertyInfo[] ps = typeof(T).GetProperties();
foreach (DataRow row in table.Rows)
{
T obj = new T();
foreach (DataColumn col in table.Columns)
{
foreach (PropertyInfo p in ps)
{
if (p.Name == col.ColumnName)
{
if (!p.PropertyType.IsGenericType)
{
p.SetValue(obj, string.IsNullOrEmpty(row[p.Name].ToString()) ? null : Convert.ChangeType(row[p.Name].ToString(), p.PropertyType));

}

else
{
if(p.PropertyType.GetGenericTypeDefinition()==typeof(Nullable<>))
{
p.SetValue(obj,string.IsNullOrEmpty(row[p.Name].ToString())?null:Convert.ChangeType(row[p.Name],Nullable.GetUnderlyingType(p.PropertyType)));
}
}
}
}
}
list.Add(obj);
}
return list;
}
}
public class Student
{
public int? StuNo { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
public override string ToString()
{
return this.StuNo + ":" + this.Name + ":" + this.Sex + ":" + this.Age;
}

}

class Program
{
public static List<Student> StuList = new List<Student>();
static void Main(string[] args)
{
StuList.Add(new Student() { StuNo = 1, Name = "1", Sex = "1", Age = 1 });
StuList.Add(new Student() { StuNo = 2, Name = "2", Sex = "2", Age = 2 });
StuList.Add(new Student() { StuNo = 3, Name = "3", Sex = "3", Age = 3 });
StuList.Add(new Student() { StuNo = 4, Name = "4", Sex = "4", Age = 4 });
StuList.Add(new Student() { StuNo = 5, Name = "5", Sex = "5", Age = 5 });
DataTable table = StuList.ToDataTable<Student>();
List<Student> stus = table.ToList<Student>();
DataTable dt2 = table.Clone();
Console.WriteLine(" dt.ToList<Student>()输出学生列表");
stus.ForEach(a => { Console.WriteLine(a.ToString()); });
foreach (DataRow odr in table.Rows)
{
DataRow ndr = dt2.NewRow();
ndr.ItemArray = odr.ItemArray;
dt2.ImportRow(odr);
}
string s = "ABC_EDF_CCS";
List<string> strList=s.Split('_').ToList();
StringBuilder sb = new StringBuilder();
foreach (string s2 in strList)
{
sb.Append(ReplaceString(s2));
}
Console.WriteLine("格式化前字符串:"+s+"格式化后字符串:"+sb.ToString());
Console.Read();
}
public static string ReplaceString(string s)
{
return Regex.Replace(s, @"([A-Za-z]{1})([A-Za-z]*)",MathcEval);
}

private static string MathcEval(Match match)

{
return match.Groups[1].Value.ToUpper() + match.Groups[2].Value.ToLower();
}
}
}

转载于:https://www.cnblogs.com/kexb/p/4556969.html

你可能感兴趣的文章
安装LAMP环境遇到Sorry, I cannot run apxs
查看>>
centos7双网卡bond失败
查看>>
JNI AES文件及字符串加解密
查看>>
APUE读书笔记-16网络通信-01简介
查看>>
企业网络安全必需措施 保证高效工作环境
查看>>
apache站点稍大文件不完整原因及解决
查看>>
python的reduce函数
查看>>
细读shell-6
查看>>
ubuntu11.10安装php mysql wordpress
查看>>
一、2 基于wsgiref定义自己的web框架
查看>>
Ubuntu Server14.04 32位安装odoo8.0简单方法
查看>>
jQuery-easyui下的多表关联的增删改操作
查看>>
C库函数学习笔记之strstr
查看>>
我的友情链接
查看>>
大数据采集、清洗、处理:使用MapReduce进行离线数据分析完整案例
查看>>
java中的自动装箱和自动拆箱
查看>>
手动创建数据库操作步骤
查看>>
yum配置与使用
查看>>
Spring+Struts 2 简单实例报空指针异常
查看>>
正则表达式测试器
查看>>