2

I use this code:

BindingFlags flags= BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; PropertyInfo prop = myObj.GetProperty("Age", flags); 

prop is not null. However, when I try to get all properties from myObj:

foreach(MemberInfo e in myObj.GetType().GetMembers( flags) ) { //neither GetProperties helps Console.WriteLine(e.Name); } 

that property (Age) is not listed. I can't understand how this happens.

9
  • 3
    Can you show the type declaration for the type of of myObj Commented May 9, 2018 at 8:23
  • 1
    I would like to see a minimal example. Commented May 9, 2018 at 8:23
  • How is Age property declared ? Commented May 9, 2018 at 8:24
  • 1
    obj is not myObj- or is that a typo? Can you show a single snippet full (minimal) example that shows this behavior? Commented May 9, 2018 at 8:25
  • Please, show us the class of your object myObj Commented May 9, 2018 at 8:29

1 Answer 1

3

The dfiference between Type.GetProperty and Type.GetMembers is that both return private properties/members(which include properties), but GetMembers only of this type and not from base types whereas GetProperty also returns private properties of base types.

GetProperty:

Specify BindingFlags.NonPublic to include non-public properties (that is, private, internal, and protected properties) in the search.

GetMembers:

Specify BindingFlags.NonPublic to include non-public members (that is, private, internal, and protected members) in the search. Only protected and internal members on base classes are returned; private members on base classes are not returned.

So i guess that Age is an inherited property. If you would add BindingFlags.DeclaredOnly the result should be the same, you wouldn't see Age.

If you want to force GetMembers to include also private members of base types, use following extension method that loops all base types:

public static class TypeExtensions { public static MemberInfo[] GetMembersInclPrivateBase(this Type t, BindingFlags flags) { var memberList = new List<MemberInfo>(); memberList.AddRange(t.GetMembers(flags)); Type currentType = t; while((currentType = currentType.BaseType) != null) memberList.AddRange(currentType.GetMembers(flags)); return memberList.ToArray(); } } 

Now your BindingFlags work already and even a private "inherited" Age property is returned:

MemberInfo[] allMembers = myObj.GetType().GetMembersInclPrivateBase(flags); 
Sign up to request clarification or add additional context in comments.

7 Comments

@T.Todua: edited my answer to show you how, use BindingFlags.FlattenHierarchy
so, I cant force GetMembers to get all things like GetProperties ?
Flatten Heirarchy only effects static members, not instance ones. Your msdn quote says as much.
@T.Todua: i have edited my answer to provide a working solution. FlattenHierarchy doesn't help with private members in base types.
@T.Todua: it changes in the loop head: while((currentType = currentType.BaseType) != null). An assignement returns the assigned value
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.