c# reflection - getting the first item out of a reflected collection without casting to specific col
Posted
by Andy Clarke
on Stack Overflow
See other posts from Stack Overflow
or by Andy Clarke
Published on 2010-04-15T16:39:52Z
Indexed on
2010/04/15
16:43 UTC
Read the original article
Hit count: 384
Hi,
I've got a Customer object with a Collection of CustomerContacts
IEnumerable Contacts { get; set; }
In some other code I'm using Reflection and have the PropertyInfo of Contacts property
var contacts = propertyInfo.GetValue(customerObject, null);
I know contacts has at least one object in it, but how do I get it out? I don't want to Cast it to IEnumerable because I want to keep my reflection method dynamic. I thought about calling FirstOrDefault() by reflection - but can't do that easily because its an extension method.
Does anyone have any ideas?
Thanks
© Stack Overflow or respective owner