I am attempting to use Reflection in C# to determine at runtime the type of objects in a collection property. These objects are entities generated by the Entity Framework:
Type t = entity.GetType();
PropertyInfo [] propInfo = t.GetProperties();
foreach(PropertyInfo pi in propInfo)
{
if (pi.PropertyType.IsGenericType)
{
if (pi.PropertyType.GetGenericTypeDefinition()
== typeof(EntityCollection<>))
// 'ToString().Contains("EntityCollection"))' removed d2 TimWi's advice
//
// ---> this is where I need to get the underlying type
// ---> of the objects in the collection :-)
// etc.
}
}
How do I identify the type of objects held by the collection?
EDIT: updated code above, adding first .IsGenericType query to make it work