reading properties of the items of an empty IList

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
 
D

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top