Determine if a property is a kind of array by reflection

后端 未结 5 621
悲&欢浪女
悲&欢浪女 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:13

    For me the following is not working,

    return property.PropertyType.GetInterface(typeof(ICollection<>).FullName) != null;
    

    Following was working,

    typeof(ICollection<>).IsAssignableFrom(property.PropertyType.GetGenericTypeDefinition())
    

    This is shortcut way to check ICollectionorICollection

    var property = request as PropertyInfo;
    
    property.PropertyType.IsGenericType && (typeof(ICollection<>).IsAssignableFrom(property.PropertyType.GetGenericTypeDefinition())) && typeof().IsAssignableFrom(property.PropertyType.GenericTypeArguments[0])
    

提交回复
热议问题