zoukankan      html  css  js  c++  java
  • 反射获取运行时属性值的替代方法

    方法一:表达式树

      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         }
  • 相关阅读:
    CF700C Break Up
    CF865C Gotta Go Fast
    CF1559D2 Mocha and Diana (Hard Version)
    CF1500C Matrix Sorting
    排列计数机
    CSP-S 2021 退役记
    【做题记录】[NOIP2011 提高组] 观光公交
    【做题记录】构造题
    乱搞
    二维树状数组
  • 原文地址:https://www.cnblogs.com/baozhu/p/4462087.html
Copyright © 2011-2022 走看看