Determine if a property is a kind of array by reflection

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

        public static bool IsGenericEnumerable(Type type)
        {
            return type.IsGenericType && 
                type.GetInterfaces().Any(
                ti => (ti == typeof (IEnumerable<>) || ti.Name == "IEnumerable"));
        }
    
        public static bool IsEnumerable(Type type)
        {
            return IsGenericEnumerable(type) || type.IsArray;
        }
    

提交回复
热议问题