vendredi 20 septembre 2019

Why does reflection over `IReadOnlyList<>` does not return the property `Count`?

Under .NET Core 2.2, when entering the following C# expression in the Immediate Window of Visual Studio 2019:

typeof(IReadOnlyList<>).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy)

I am getting the following return:

{System.Reflection.PropertyInfo[1]}
     [0]: {T Item [Int32]}

This is rather odd, because I would expect the IReadOnlyCollection<>.Count property to returned due to the BindingFlags.FlattenHierarchy flag.

Also calling typeof(IReadOnlyList<>).GetInterfaces() returns:

{System.Type[3]}
    [0]: {Name = "IReadOnlyCollection`1" FullName = null}
    [1]: {Name = "IEnumerable`1" FullName = null}
    [2]: {Name = "IEnumerable" FullName = "System.Collections.IEnumerable"}

And typeof(IReadOnlyList<>).GetInterfaces()[0].GetProperties() returns:

{System.Reflection.PropertyInfo[1]}
    [0]: {Int32 Count}

I am a bit at a loss here. Could someone explains why isn't the Count property returned as per my first statement above?





Aucun commentaire:

Enregistrer un commentaire