1

I am trying to understand reflection. I am trying to use reflection to get the properties from objects. I first was using

var propertiesForManuallyCreated = typeof(T).GetProperties();

but then realized it wasn't getting the base properties. I tried

var propertiesForManuallyCreatedBase = typeof(T).BaseType.GetProperties();

but that didn't get me the base properties. How can I get the base properties? Below is an image of what I am talking about when I say base.

Local Image

Dan Burgener
  • 796
  • 7
  • 16

1 Answers1

6

You need to use the Type.GetProperties(BindingFlags) overload with a value of BindingFlags.FlattenHierarchy as the parameter.

var propertiesForManuallyCreated =
    typeof(T).GetProperties(BindingFlags.FlattenHierarchy);

Per MSDN:

FlattenHierarchy

Specifies that public and protected static members up the hierarchy should be returned. Private static members in inherited classes are not returned. Static members include fields, methods, events, and properties. Nested types are not returned.

Community
  • 1
  • 1
Adam Maras
  • 26,269
  • 6
  • 65
  • 91