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...
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
IEnumerableby usingtypeof(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 ifproperty.PropertyType.GetInterface(typeof(IEnumerable<>).FullName)is notnull. Note that I didn’t specify any type forTin that code. You can do the same forIList<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>:Arrays also implement IEnumerable, so they will also return
truefrom that method.