Type.GetProperties Method Returns a PropertyInfo[] But PropertyInfo is Abstract

55 views Asked by At

In the documentation for C# the GetProperties method of the Type class returns an array of Property Infos: System.Reflection.PropertyInfo[]. You can see this in the documentation here:

https://learn.microsoft.com/en-us/dotnet/api/system.type.getproperties?view=net-8.0

However, the PropertyInfo class is marked as abstract in the documentation:

https://learn.microsoft.com/en-us/dotnet/api/system.reflection.propertyinfo?view=net-8.0

Wondering how this is possible since abstract classes cannot be instantiated in c#. Would this be a typo in the documentation or is it returning something else?

1

There are 1 answers

2
NPras On BEST ANSWER

You're correct that PropertyInfo is abstract. The thing is, GetProperties() actually returns RuntimePropertyInfo (a child class of PropertyInfo), which is not abstract. It is internal sealed so you still can't instantiate it, but the runtime can.

foreach (var p in typeof(Exception).GetProperties())
    Console.WriteLine($"{p.Name} - {p.GetType().FullName} - {p.GetType().IsAbstract}");