tags:

views:

124

answers:

1

I have a class which has some properties of type List<float>, List<int> etc. Now I am quering the properties of this class through reflection so that I get a list of PropertyInfo.

I want to filter the types which are of type List<>. But the comparison

propertyInfo.PropertyType == typeof(List<>)

fails.

I can get around this by comparing the names, i.e., the following comparison works:

propertyInfo.PropertyType.Name == typeof(List<>).Name

I think there should be a better way to compare the Generic types. Any clues?

+13  A: 

You can use:

Type type = propertyInfo.PropertyType;
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
{
    ...
}
Jon Skeet