Using reflection I can obtain the object properties and values for all Data Types and the objects inside this object. But if the object contains a List of other objects I am having trouble in getting the objects in the list. The list can contain any type of object.
private static void SaveObj(object obj) {
foreach (var prop in obj.GetType().GetProperties()) {
if (prop.PropertyType.Namespace == "Entities") { //It is an object
object obL = prop.GetValue(obj, null);
SaveObj(obj);
}
else if (prop.PropertyType.Name == "List`1") { //This is a list of objects
object obP = prop.GetValue(obj);
//obP has the list of objects, I can see the list in debug mode.
List<object> obL = (List<object>)prop.GetValue(obj, null);
//This line returns an exception!!
}
else {
columns += prop.Name.ToLower() + ", ";
values[i] = prop.GetValue(obj, null).ToString();
}
... // the code continues ...
}
}
The exception message returned is: "It is not possible to convert an object of type 'System.Collections.Generic.List1[Entities.OrderItem]' to type 'System.Collections.Generic.List
1[System.Object]'."
Interesting is that I can see all the objects and its contents in degug mode. In Immediate Window I can print the content of the variable obP with all the objects in the list, but how to read them?
Any ideas on how to solve this?
Aucun commentaire:
Enregistrer un commentaire