How to access the PropertyInfo of Generic List collection using Reflection ?

Hi,

I have some class objects like

public class Order
{
public DateTime OrderDate
{
get
{
return this.m_tOrderDate;
}
set
{
this.m_tOrderDate = value;
}
}
public List<OrderDetail> OrderDetailList
{
get
{
return this.m_arlDetail;
}
set
{
this.m_arlDetail = value;
}
}

I can access the property like order date using System.Reflection.

PropertyInfo[] Properties = t.Data.GetType().GetProperties();

foreach (PropertyInfo p in Properties)

{

Console.Writeline(p.GetValue(t.Data,null).ToString());

}

public class OrderDetail

{

// Properties here

}

However, I cannot access the objects like OrderDetail using the above code... What I need is I like to list out all the orderdetail objects and display the value of each properties.

Can anybody help me please




Answer this question

How to access the PropertyInfo of Generic List collection using Reflection ?

  • Sam Hobbs

    This one way to do it, I guess. t is assumed to be the order object.

    PropertyInfo propertyInfo = t.GetType().GetProperty("OrderDetailList");

    object list = propertyInfo.GetValue(t, null);

    List<OrderDetail> details = (List<OrderDetail>)list;

    foreach (OrderDetail od in details)

    {

    // Handle the detail

    }



  • J.X.Q.

    Hi Andreas,

    I got it ......now as follows;

    PropertyInfo p1 = o1.GetType().GetProperty("OrderDetailList");
    System.Collections.
    IList a = (System.Collections.IList)p1.GetValue(o1, null);
    PropertyInfo g = a[0].GetType().GetProperty("Qty");

    Thank you very much for your efforts.....



  • siavoshkc

    Hi,

    Thank you. However, I like to have a dynamic solution instead of this one. because t can bey any of Order/ PurchaseVoucher/ some others...

    PropertyInfo propertyInfo = t.GetType().GetProperty("OrderDetailList");

    object list = propertyInfo.GetValue(t, null);

    ///Instead of this...
    //List<OrderDetail> details = (List<OrderDetail>)list;
    //foreach (OrderDetail od in details)
    //{
    // Handle the detail
    //}

    // I like to :
    // 1. Get the Collection object and assign to one dynamic object varialbe
    // 2. List of all the List<> items
    // 3. When you choose one List<> item, list of all the property names of a specific item
    // and the values.
    // Generally, it is more like the Autos Window in VS.NET 2005.

    I have tried the following;

    //objParam[0] = (object)intIndex;
    //object objValue = objProp.GetValue(objCol, objParam)

    but I got "Parameter count mismatch" exception.



  • How to access the PropertyInfo of Generic List collection using Reflection ?