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]
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}));
}
}