Search code examples
c#observablecollectionilist

Why doesn't ObservableCollection implement IList?


I have a Type of {System.Collections.ObjectModel.ObservableCollection} and it implements the following interfaces

enter image description here

This is my code to check if the type implements IList

if (type.IsGenericType && type.GetInterfaces().Contains(typeof(IList<>)))
{
    type = type.GetGenericArguments()[0];
    enclosedType = EnclosedType.List;
}

Why doesn't this work? I feel like I'm missing something obvious.


Solution

  • As stated below. It implements IList<TheType> and not IList You need to check the generic type definitions for the interfaces.

    Example:

    type.GetInterfaces()
        .Where(i => i.IsGenericType)
        .Select(i => i.GetGenericTypeDefinition())
        .Contains(typeof(IList<>));