C# 通过反射确定属性是否是一种数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9434825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:24:28  来源:igfitidea点击:

Determine if a property is a kind of array by reflection

c#reflection

提问by Melursus

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

如何确定一个属性是否是一种数组。

Example:

例子:

public bool IsPropertyAnArray(PropertyInfo property)
{
    // return true if type is IList<T>, IEnumerable<T>, ObservableCollection<T>, etc...
}

采纳答案by Sven

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

您似乎在问两个不同的问题:类型是数组(例如string[])还是任何集合类型。

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

对于前者,只需检查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 IEnumerableby 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).

对于后者,您必须决定您希望类型符合的最低标准是什么。例如,您可以IEnumerable使用typeof(IEnumerable).IsAssignableFrom(property.PropertyType). 如果您知道 T 的实际类型,您也可以将其用于通用接口,例如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 Tin that code. You can do the same for IList<T>or any other type you're interested in.

IEnumerable<T>不知道 T 的值的情况下检查泛型或任何其他泛型接口可以通过检查 if property.PropertyType.GetInterface(typeof(IEnumerable<>).FullName)is not来完成null。请注意,我没有T在该代码中指定任何类型。您可以对IList<T>您感兴趣的任何其他类型或任何其他类型执行相同的操作。

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

例如,如果您想检查泛型,您可以使用以下内容IEnumerable<T>

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

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

数组也实现了 IEnumerable,因此它们也会true从该方法返回。

回答by Falanwe

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:

如果你想知道它是否是一个实现 IEnumerable 的类型,就像所有“集合类型”一样,它也不是很复杂:

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

回答by gabnaim

    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;
    }

回答by Sabarinathan Arthanari

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>

这是检查ICollection<IInterface>ICollection<BaseClassInTree>

var property = request as PropertyInfo;

property.PropertyType.IsGenericType && (typeof(ICollection<>).IsAssignableFrom(property.PropertyType.GetGenericTypeDefinition())) && typeof().IsAssignableFrom(property.PropertyType.GenericTypeArguments[0])

回答by Furqan Safdar

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

排除String类,因为它符合集合的条件,因为它实现了IEnumerable<char>.

public bool IsPropertyACollection(this PropertyInfo property)
{
    return (!typeof(String).Equals(property.PropertyType) && 
        typeof(IEnumerable).IsAssignableFrom(property.PropertyType));
}