Point 1: I have the below class structure
public class MainModel
{
public List<employee> Employees { get; set; }
public List<department> Departments { get; set; }
}
[Serializable]
public class Employee
{
public Int64 EmpId { get; set; }
public string EmpName { get; set; }
public DateTime? DOB { get; set; }
public decimal? Salary { get; set; }
}
[Serializable]
public class Department
{
public Int64 DeptID { get; set; }
public string DeptName { get; set; }
public List<employee> Employees { get; set; }
}
Point2: All the class are loaded with some data.
Point3 I have an object of MainModel as mainModel
Point 4 I have written the below code to get the data dynamically from the list properties of MainModel class and store it in generic list variable, and finally I have to use that list at some other place.
Type objType = mainModel.GetType();
PropertyInfo[] properties = objType.GetProperties();
PropertyDescriptorCollection propAttributes = TypeDescriptor.GetProperties(typeof(MainModel));
int i = 0;
foreach (PropertyInfo property in properties)
{
PropertyDescriptor prop = propAttributes[i];
string propName = prop.Name;
Type type = prop.PropertyType;
object propValue = property.GetValue(mainModel);
IEnumerable Data = propValue as IEnumerable;
}
Point 5 If you look at the above code, I have a variable named propValue of object type, In that variable, on the basis of iteration values propValue variable gets populated with data of List < Employees> and List< Department>
Note: I have to get value of
propValue variable and store it in some kind of generic list, so that I can pass it to some method which accept any type of list.
Please suggest