清单& T到字典 [英] List<T> to a Dictionary

查看:39
本文介绍了清单& T到字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 List< CustomObject> 具有3个属性A,B,C我需要的是将此列表转换为字典,使结果看起来像

I have a List<CustomObject> which has 3 properties A,B,C and what I need is to transform this List to a Dictionary so the result looks like

Dictionary<string,object>
(Property name) A = Value of A
(Property name) B = Value of B
(Property name) C = Value of C

建议...

推荐答案

CustomObject instance = new CustomObject();
var dict = instance.GetType().GetProperties()
    .ToDictionary(p => p.Name, p => p.GetValue(instance, null));

这篇关于清单&amp; T到字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆