从c#expando对象动态读取属性 [英] Dynamically read properties from c# expando object

查看:194
本文介绍了从c#expando对象动态读取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有以下方法 -

Currently I have the following method-

public void CreateWorkbook<T>(List<T> ItemList)
{
     PropertyInfo[] properties= typeof(T).GetProperties();
     foreach (PropertyInfo prop in properties)
     {
     }
}

我想用expando对象替换 T 。但是我不能从expando对象读取属性。

I would like to replace T with expando object. But I can't read properties from an expando object.

有什么办法吗?

推荐答案

你可以通过使用以下方式获取属性:

You can get the properties by using something like:

public static void CreateWorkbook(List<ExpandoObject> ItemList)
        {
            foreach(var item in ItemList)
            {
                IDictionary<string, object> propertyValues = item;

                foreach (var property in propertyValues.Keys)
                {
                    Console.WriteLine(String.Format("{0} : {1}", property, propertyValues[property]));
                } 
            }
        }

这篇关于从c#expando对象动态读取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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