Reputation: 13788
I am writing something that needs to discover the types of all the properties in an object at runtime, using reflection. I'm fine with simple properties, I just get the PropertyInfo
and the type is directly available. I can't work out what to do for generic collections, though. For example, suppose I am handed an instance of the following class at runtime:
public class AnyClass
{
public ICollection<int> ListOfInts;
}
So I use Type.GetProperties
and pretty soon I have my PropertyInfo
object for ListOfInts
.
What's my next step? How do I go from having the PropertyInfo, to working out that it is a list of ints? How can I determine the generic type (<int>
in this example case) of the collection from just the PropertyInfo
?
Upvotes: 0
Views: 83
Reputation: 100547
I believe you are looking for generic arguments of the type. See PropertyInfo.PropertyType and Type.GenericTypeArguments
PropertyInfo.PropertyType.GenericTypeArgument
Upvotes: 2
Reputation: 65079
You can get the name of the generic argument type:
propertyInfo.PropertyType.GetGenericArguments()[0].Name
Upvotes: 2