0

I have 2 classes (lets say "BaseItem" and "ChildItem") with several internal properties. In the BaseClass i have defined a method which should read out all of this properties with

PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this);

or

PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this.GetType()); 

When i call this method in an instance of "ChildItem", i get only the properties that are defined in "ChildItem". What can i do to get also the the properties of "BaseItem"?

Regards

Dave

Marcel N.
  • 13,726
  • 5
  • 47
  • 72
  • Why are you using `TypeDescriptor` and not `Type` directly to get the props? – Marcel N. Aug 11 '14 at 05:57
  • You can have a look at this: http://stackoverflow.com/questions/245055/how-do-you-get-the-all-properties-of-a-class-and-its-base-classes-up-the-hierar – Marcel N. Aug 11 '14 at 05:59
  • I tried this with Type.GetProperties also. The result is the same. Just properties defined in "ChildItem" will be returned. – user2767511 Aug 11 '14 at 06:00
  • Are the properties you want from base public or private/protected/internal? – Marcel N. Aug 11 '14 at 06:01

2 Answers2

0

You could just cast it to BaseItem and do GetProperties() separately..

bit
  • 4,407
  • 1
  • 28
  • 50
  • That is my current workaround. I iterate through all base classes till i reach a specified one ("BaseItem). But i am really sure, there is a much faster solution available ;-) – user2767511 Aug 11 '14 at 06:02
0

For internal properties (as clarified in the question comments), although not indicated (they're internal for a reason), you can use:

var internalProperties = GetType().GetProperties(
                            BindingFlags.Instance | 
                            BindingFlags.NonPublic |
                            BindingFlags.Public));

It's the flag BindingFlags.NonPublic that needs to be applied.

Marcel N.
  • 13,726
  • 5
  • 47
  • 72