Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing an C# library application. My requirement is to define an function which takes List<Object> as an argument and parse each element in it.
ParseList(List<Object> objTestResults)

Each item in the List is instance of DTO class DTO_Employee(with name, second name etc as fields), DTO_Management etc. Before calling the ParseList function, the caller converts the List<dto_employee> or List<dto_management> to List<Object>.

Is it possible to get the value of each field in item from objTestResults.
Ex : If List<Object> has 3 items of DTO_Employee(typecast as object type). I would like to access the name, second name etc of all the employees in the list.

During run time my dll wont be knowing what data type items the list holds(dll wont be knowing if the items are of DTO_Employee or DTO_Management). I tried solving the same using System.Reflection namespace classes, but was not successful.

Any help/direction is appreciated.

[CODE FROM OP'S COMMENT]
C#
object value = new object();

foreach (var item in objTestResults)
{
  Type type = item.GetType();
  PropertyInfo[] propertyList = type.GetProperties();
  foreach (var property in propertyList)
  {
    Console.WriteLine(property.GetValue(value, new object[] {property}));
  }
}
Posted
Updated 12-Jan-15 22:37pm
v2
Comments
Kornfeld Eliyahu Peter 13-Jan-15 4:25am    
All the possible types are known types?
SaiprasadVer 13-Jan-15 4:28am    
Nope... i wont be knowing the Type that is in the List. My function should get the properties and thier respective values.
Kornfeld Eliyahu Peter 13-Jan-15 4:30am    
So the way is reflection...Can you show us the code, that 'was not successful'?
SaiprasadVer 13-Jan-15 4:35am    
Code below

object value = new object();
foreach (var item in objTestResults)
{
Type type = item.GetType();
PropertyInfo[] propertyList = type.GetProperties();
foreach (var property in propertyList)
{
Console.WriteLine(property.GetValue(value, new object[] {property}));
}

}

I am figuring it out how i can achieve this. For now, the above code is what i have.
Kornfeld Eliyahu Peter 13-Jan-15 4:38am    
It's a fair code for beginning - but does not explain what's your problem!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900