你似乎在问两个不同的问题:一个类型是一个数组(例如string [])还是任何集合类型。
对于前者,只需检查property.PropertyType.IsArray。
对于后者,您必须确定您希望类型符合的最低标准是什么。例如,可以使用typeof(IEnumerable).IsAssignableFrom(property.PropertyType)来检查非泛型的IEnumerable。如果您知道T的实际类型,也可以将其用于通用接口。 typeof(IEnumerable< int>)。IsAssignableFrom(property.PropertyType)。
检查通用IEnumerable< T>或任何其他通用接口,而不知道T的值可以通过检查property.PropertyType.GetInterface(typeof(IEnumerable?)。FullName)不为null来完成。请注意,我没有在该代码中为T指定任何类型。您可以对IList T进行相同的操作。或您感兴趣的任何其他类型。
例如,如果要检查通用的IEnumerable< T> ;::::可以使用以下内容:
public bool IsPropertyACollection(PropertyInfo property)
{
return property.PropertyType.GetInterface(typeof(IEnumerable<>).FullName) != null;
}
数组还实现IEnumerable,所以它们也将从该方法返回true。