方法一:表达式树
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 }
时间: 2024-09-28 03:43:24