Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Point 1: I have the below class structure
C#
//First Class, which is having MainModel
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.

C#
Type objType = mainModel.GetType();
PropertyInfo[] properties = objType.GetProperties();

PropertyDescriptorCollection propAttributes = TypeDescriptor.GetProperties(typeof(MainModel));
int i = 0;

foreach (PropertyInfo property in properties)
{
    //Description about properties
    PropertyDescriptor prop = propAttributes[i];
    string propName = prop.Name;
    Type type = prop.PropertyType;
    
    //Data of properties
     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
Posted
v5
Comments
Nathan Minier 11-Dec-15 10:44am    
Well, it certainly doesn't help that you're using a for loop control in a foreach. You never actually increment i. Make it a standard for loop and you'll have better luck.

I'm a little confused by your use case. Passing completely generic IEnumerable<object> is not a great solution, if you want to make things interchangeable, apply an interface to them and make the list against that interface, such as:
public interface IMyLists{}. That will at least keep arbitrary objects from being injected into your methods.
Sergey Alexandrovich Kryukov 11-Dec-15 15:24pm    
It's totally unclear to me why would you decide to use reflection on this type.
It is not clear how is that related to your only question, the one formulated in the question title.
It's obvious that you are not even trying to store the object propValue anywhere. You just get the value, and it gets lost after each iteration.
You already have two lists in the object instance; so why would you need something else? If you need to create another list, populate it, why not doing exactly that?
—SA

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