reading properties of the items of an empty IList

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

I have to read the property names of the items of an IList to show them in a
DataGridView.
This is no problem if the passed IList contains items, so I can select the
first item to get it's type and then get it's properties, like:

void CreateDataGridViewColumns( IList list )
{
foreach( PropertyInfo pi in list[ 0 ].GetType().GetProperties() )
{
DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.Name = pi.Name
dataGridView.Add( column );
}
}

But how can I get the type of the IList items if tht IList is empty?

Thanks in advance
Daniel
 
Thanks Pete,
If the implementing type happens to be a generic collection type (and so
there _can_ be a guarantee about the type of objects in the collection),
then you can use GetType() on the IList reference itself, and call
GetGenericArguments() to retrieve the type parameter used when
declaring/creating the instance of the IList implementor:

Your assumption about the generic collection type was absolutely correct.
This solved my problem:

foreach( PropertyInfo pi in ( list.GetType().GetGenericArguments()[
0 ] ).GetProperties() )
{
...
}

Thanks again
Daniel
 
Back
Top