开发者

How to use linq functions like Sum() with dynamic variable

开发者 https://www.devze.com 2023-03-03 14:21 出处:网络
Type t = Type.GetType开发者_开发百科(obj.ToString()); PropertyInfo p = t.GetProperty(\"Test\");
Type t = Type.GetType开发者_开发百科(obj.ToString());                 
PropertyInfo p = t.GetProperty("Test");
dynamic kk =  p.GetValue(obj, null);

In this, Test is an int List. I need to get the sum of all the values from int list ie., kk.


var list = p.GetValue(obj, null) as IEnumerable<int>;

var result = list.Sum();


// Type t = Type.GetType(obj.ToString());
Type t = obj.GetType(); // You might be able to replace the above line with this, simpler verison.
PropertyInfo p = t.GetProperty("Test");
object kk =  p.GetValue(obj, null);
int Sum = (kk as List<int>).Sum();
0

精彩评论

暂无评论...
验证码 换一张
取 消