Determine if a property is a kind of array by reflection

后端 未结 5 599
悲&欢浪女
悲&欢浪女 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;
        }
    
    0 讨论(0)
  • 2020-12-24 02:08

    You appear to be asking two different questions: whether a type is an array (e.g. string[]) or any collection type.

    For the former, simply check property.PropertyType.IsArray.

    For the latter, you have to decide what is the minimum criteria you want a type to conform to. For example, you could check for the non-generic IEnumerable by using typeof(IEnumerable).IsAssignableFrom(property.PropertyType). You can also use this for generic interfaces if you know the actual type of T, e.g. typeof(IEnumerable<int>).IsAssignableFrom(property.PropertyType).

    Checking for the generic IEnumerable<T> or any other generic interface without knowing the value of T can be done by checking if property.PropertyType.GetInterface(typeof(IEnumerable<>).FullName) is not null. Note that I didn't specify any type for T in that code. You can do the same for IList<T> or any other type you're interested in.

    For example you could use the following if you want to check for the generic IEnumerable<T>:

    public bool IsPropertyACollection(PropertyInfo property) 
    { 
        return property.PropertyType.GetInterface(typeof(IEnumerable<>).FullName) != null;
    } 
    

    Arrays also implement IEnumerable, so they will also return true from that method.

    0 讨论(0)
  • 2020-12-24 02:09

    Excluding String class as it qualifies as a collection because it implements IEnumerable<char>.

    public bool IsPropertyACollection(this PropertyInfo property)
    {
        return (!typeof(String).Equals(property.PropertyType) && 
            typeof(IEnumerable).IsAssignableFrom(property.PropertyType));
    }
    
    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 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 ICollection<IInterface>orICollection<BaseClassInTree>

    var property = request as PropertyInfo;
    
    property.PropertyType.IsGenericType && (typeof(ICollection<>).IsAssignableFrom(property.PropertyType.GetGenericTypeDefinition())) && typeof().IsAssignableFrom(property.PropertyType.GenericTypeArguments[0])
    
    0 讨论(0)
提交回复
热议问题