今天上班不太忙,就想着总结一下反射、扩展方法、以及lambda表达式的用法,自己就写了个小DEMO记录一下,希望各位大牛们看到后觉得不对的地方请及时提出。这篇文章中我只说明我的用法,作为一个备忘,基本的语法我不讲解,对这些概念不熟悉的童鞋在博客园上搜素一下,呢那个找到很多相关的讲解,小弟就先抛砖引玉了。
另附链接一枚,如果你想知道c#1.0--c#4.0 主要基础知识汇总,请猛点下面链接,你还可以看到该片文章中提到的知识点。
猛点我
详细见我的163博客.NET反射详解
引MSDN对扩展方法的定义: 扩展方法使你能够向现有类型“添加”方法(包括你自定义的类型和对象噢),而无需创建新的派生类型、重新编译或以其他方式修改原始类型。扩展方法是一种特殊的静态方法,但是可以像扩展类型上的实例方法一样进行调用。对于用C#编写的客户端代码,调用扩展方法与调用在类型中实际定义的方法之间没有明显的差异。
引百度百科对Lambda的定义“Lambda 表达式”是一个匿名函数,它可以包含表达式和语句,并且可用于创建委托或表达式目录树类型。 所有 Lambda 表达式都使用 Lambda 运算符 =>,该运算符读为“goes to”。该 Lambda 运算符的左边是输入参数(如果有),右边包含表达式或语句块。
说了这么多,开始上代码。
方法一:扩展方法与反射实现 DataTable 转 List<T>
public static List<T> ToList<T>(this DataTable dt) where T:class,new(){Type t=typeof(T);PropertyInfo[] propertys = t.GetProperties();List<T> lst = new List<T>();string typeName = string.Empty;foreach (DataRow dr in dt.Rows){T entity = new T();foreach (PropertyInfo pi in propertys){typeName = pi.Name;if (dt.Columns.Contains(typeName)){if (!pi.CanWrite) continue;object value = dr[typeName];if (value == DBNull.Value) continue;if (pi.PropertyType == typeof(string)){pi.SetValue(entity,value.ToString(),null);}else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?)){pi.SetValue(entity,int.Parse(value.ToString()), null);}else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime)){pi.SetValue(entity, DateTime.Parse(value.ToString()), null);}else if (pi.PropertyType == typeof(float)){pi.SetValue(entity, float.Parse(value.ToString()), null);}else if (pi.PropertyType == typeof(double)){pi.SetValue(entity, double.Parse(value.ToString()), null);}else{pi.SetValue(entity,value, null);}}}lst.Add(entity);}return lst;}
方法一调用
(1)首先创建一个实体类
public class People{public string Name { get; set; }public int Age{get;set;}}
(2)调用
DataTable dt = new DataTable();dt.Columns.Add("Name");dt.Columns.Add("Age");DataRow dr = dt.NewRow();dr[0] = "eric";dr[1] = 20;dt.Rows.Add(dr);dr = dt.NewRow();dr[0] = "eric1";dr[1] = 22;dt.Rows.Add(dr);List<People> p = dt.ToList<People>();
方法二:扩展方法与Action委托实现数组的遍历操作(ForEach)
public static void ForEach<T>(this IEnumerable<T> ien,Action<T> express){foreach (var item in ien){express(item);}}
方法二调用
List<string> lst = new List<string>();var arr = new string[3] { "eric01", "eric02", "eric03" };arr.ForEach(it => lst.Add(it));//这里面可以做更复杂的处理Response.Write(lst[0]);
方法三:利用方法一和方法二实现List<t>转换DataTable
public static DataTable ToDataTable<T>(this IEnumerable<T> value) where T : class,new(){List<PropertyInfo> lstProperty = new List<PropertyInfo>();Type type=typeof(T);DataTable dt = new DataTable();type.GetProperties().ForEach(p => //ForEach扩展方法,这里使用Array.ForEach(type.GetProperties(),p=>{})也是一样 {lstProperty.Add(p);if (p.PropertyType.IsGenericType)//是否为泛型,泛型获取不到具体的类型 {dt.Columns.Add(p.Name);}else{dt.Columns.Add(p.Name,p.PropertyType);}});if (value != null){foreach (var item in value){//创建一个DataRow实例DataRow row = dt.NewRow();lstProperty.ForEach(p =>{row[p.Name] = p.GetValue(item, null);});dt.Rows.Add(row);}}return dt;}
OK了 今天就到此为止了,睡觉喽。每天学习一点点,每天进步一点点。
联系客服