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
?
您可以获取通用参数类型的名称:
propertyInfo.PropertyType.GetGenericArguments()[0].Name
I believe you are looking for generic arguments of the type. See PropertyInfo.PropertyType and Type.GenericTypeArguments
PropertyInfo.PropertyType.GenericTypeArgument
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.