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

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

问题描述

我有一个列表< CustomObject> 其中有3个属性

A,B,C
和我需要的是改造这个列表字典的一个这样的结果看起来像

 词典<字符串对象> 
(属性名)A A
(物业名称)= B值的B
=值(属性名称)C
C =值

PLS建议...


解决方案

  CustomObject实例=新CustomObject(); 
VAR字典= instance.GetType()的GetProperties()
.ToDictionary(P => p.Name,P => p.GetValue(例如,NULL))。


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

Pls suggest...

解决方案

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

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

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