堡主大名花花

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

方法一:表达式树

  1         public static Dictionary<string, Func<T, object>> InitDic<T>()

 2         {
 3             Dictionary<string, Func<T, object>> model = new Dictionary<string, Func<T, object>>();
 4             var plist = typeof(T).GetProperties().ToList();
 5             var newa = Expression.Parameter(typeof(T), "c");
 6 
 7             foreach (var item in plist)
 8             {
 9                 var callfoo = Expression.Property(newa, typeof(T).GetProperty(item.Name));
10                 var convert = Expression.Convert(callfoo, typeof(object));
11                 var func = Expression.Lambda<Func<T, object>>(convert, newa).Compile();
12                 model.Add(item.Name, func);
13             }
14 
15             return model;
16         }
posted on 2015-04-28 09:19  堡主大名花花  阅读(168)  评论(0编辑  收藏  举报