Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically read properties from c# expando object [duplicate]

Currently I have the following method-

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

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

is there any way to do it?

like image 604
s.k.paul Avatar asked Jan 21 '17 14:01

s.k.paul


1 Answers

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]));
                } 
            }
        }
like image 192
Ivaylo Stoev Avatar answered Nov 16 '22 02:11

Ivaylo Stoev