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();
}
}
}

ToList和ToDataTable(其中也有反射的知识)的更多相关文章

  1. C#反射基础知识和实战应用

    首先来说一下什么是反射? 反射提供了封装程序集.模块和类型的对象(Type类型) 可以使用反射动态的创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型,然后,可以调用类型的方法或访问其字段和 ...

  2. Java反射-高级知识掌握

    PS:本文就Java反射的高级知识做下汇总,理清在什么情况下,我们应该去使用反射,提供框架的健壮性,ps:xieyang@163.com/xieyang@163.com

  3. Java反射-中级知识掌握

    PS:本文就Java反射常用的中级知识做下汇总和分析/cnxieyang@163.com/xieyang@e6yun.com

  4. Java反射基础知识

    反射机制就是可以把一个类,类的成员(属性.方法)当成一个对象来操作,也就是说,类,类的成员,我们在运行的时候可以动态的去操作它们. 所有的Java类都继承了Object类,在Object类中定义了一个 ...

  5. java反射基础知识(五)反射应用实践

    详解Java反射各种应用   Java除了给我们提供在编译期得到类的各种信息之外,还通过反射让我们可以在运行期间得到类的各种信息.通过反射获取类的信息,得到类的信息之后,就可以获取以下相关内容: Cl ...

  6. java反射基础知识(四)反射应用实践

    反射基础 p.s: 本文需要读者对反射机制的API有一定程度的了解,如果之前没有接触过的话,建议先看一下官方文档的Quick Start. 在应用反射机制之前,首先我们先来看一下如何获取一个对象对应的 ...

  7. java反射基础知识(三)

    原文地址:http://tutorials.jenkov.com/java-reflection/index.html http://www.cnblogs.com/penghongwei/p/329 ...

  8. java反射基础知识(二)

    1. 了解 Java 中的反射 1.1 什么是 Java 的反射 Java 反射是可以让我们在运行时获取类的函数.属性.父类.接口等 Class 内部信息的机制.通过反射还可以让我们在运行期实例化对象 ...

  9. java反射基础知识(一)

    一.反射 反射:JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为 ...

随机推荐

  1. Js 实战3(实现全选)

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs ...

  2. 【java多线程】java的线程池分析

    (一)线程池的拒绝策略 --->拒绝策略的接口java.util.concurrent.RejectedExecutionHandler --->终止策略(默认):java.util.co ...

  3. LG4717 【模板】快速沃尔什变换

    题意 题目描述 给定长度为\(2^n\)两个序列\(A,B\),设\(C_i=\sum_{j\oplus k}A_jB_k\)分别当\(\oplus\)是or,and,xor时求出C 输入输出格式 输 ...

  4. IMP-00009: 导出文件异常结束 imp

    在一次exp/imp中,用imp导入数据时报错.错误信息如下: IMP-00009: 导出文件异常结束 imp导入时异常结束可以有很多原因造成,要具体问题具体分析. 可能原因一: 导入的数据表过大,而 ...

  5. FastAdmin 升级后出现 is already in use

    FastAdmin 升级后出现 is already in use 升级 FastAdmin 改进很多,但全新安装出现以下错误 Cannot use app\common\library\Menu a ...

  6. ffmpeg采集帧出错不退出的补丁

    在ffmpeg2.81.11和ffmpeg3.0.7上试验.ffmpeg没有FFERROR_REDO常量定义,但ffmpeg3.0.7上有. diff --git a/libavdevice/v4l2 ...

  7. redhat 连接mysql数据库Can't get hostname for your address

    redhat 连接mysql数据库Can't get hostname for your address Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQ ...

  8. 【ZZ】C++静态库与动态库 | 菜鸟教程

    C++静态库与动态库 http://www.runoob.com/w3cnote/cpp-static-library-and-dynamic-library.html

  9. java版本特性总结

    学java这么久,对其每个版本的特性不是怎么了解,今天总结一下. 1.4: java NIO,基于多路复用技术(基于IO) 1.5 枚举.foreach.static导入 范型(重要) 注解(配置文件 ...

  10. python入门第3篇 pycharm安装及使用

    内容: 1. python开发工具的介绍及安装 2.pycharm的设置及技巧 一.python开发工具的介绍及安装 python下载后就自带了一个官方的IDE,官方的IDE我个人觉得不是很好用,所以 ...