Determine if a property is a kind of array by reflection

后端 未结 5 620
悲&欢浪女
悲&欢浪女 2020-12-24 01:46

How can I determine if a property is a kind of array.

Example:

public bool IsPropertyAnArray(PropertyInfo property)
{
    // return true if type is I         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 02:11

    If you want to know if the property is an array, it's actually very easy:

    property.PropertyType.IsArray;
    

    edit

    If you want to know if it's a type that implements IEnumerable, as do all "collection types", it's not very complicated either:

    return property.PropertyType.GetInterface("IEnumerable") != null;
    

提交回复
热议问题