ToList和ToDataTable(其中也有反射的知识)

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