Type.GetMembers()
-
I've been using System.Reflection a lot for my custom serializer/deserializer classes, and I'm really digging it. It has made a lot of it pretty easy. So I've been using
obj.GetType().GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
to get all the members I care about -- fields and properties. What's weird, though, is that GetMembers() isn't returning the type's inherited members. I expect it should, and I believe that a few days ago it was. I'm not using any other binding flags, especially not one that would limit the search to just members declared in the immediate type. Am I doing something wrong, or is the expected usage of GetMembers() that I'd have to iteratively traverse the inheritance tree and GetMembers() of each inherited type? Thanks, Arun -
I've been using System.Reflection a lot for my custom serializer/deserializer classes, and I'm really digging it. It has made a lot of it pretty easy. So I've been using
obj.GetType().GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
to get all the members I care about -- fields and properties. What's weird, though, is that GetMembers() isn't returning the type's inherited members. I expect it should, and I believe that a few days ago it was. I'm not using any other binding flags, especially not one that would limit the search to just members declared in the immediate type. Am I doing something wrong, or is the expected usage of GetMembers() that I'd have to iteratively traverse the inheritance tree and GetMembers() of each inherited type? Thanks, ArunUnless
BindingFlags.DeclaredOnly
is set, you should be seeing all inheritted, yes. May I ask how you determined that the inherited members weren't being returned? Perhaps surrounding code would be helpful.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Unless
BindingFlags.DeclaredOnly
is set, you should be seeing all inheritted, yes. May I ask how you determined that the inherited members weren't being returned? Perhaps surrounding code would be helpful.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Right. I'm definitely not using BindingFlags.DeclaredOnly. I know that inherited members aren't being returned because they're not being serialized (the output is XML), and I can even check by stepping through with a debugger. I'm not at work right now, but the code is basically:
MemberInfo[] members = obj.GetType().GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach (Member member in members) { if (member is FieldInfo) { ... } else if (member is PropertyInfo) { ... } }
Inside each block, I store the member if the conditions are proper. But even if I remove those conditions, I'm still only getting the non-inherited types. I'm a little concerned that a previous version of my assembly is being used. I'll check for that on Monday, but again, I doubt that's the case since I was even stepping through with VS. -
Unless
BindingFlags.DeclaredOnly
is set, you should be seeing all inheritted, yes. May I ask how you determined that the inherited members weren't being returned? Perhaps surrounding code would be helpful.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Well, it looks like I can't get access to private fields of inherited types through GetMembers(). I guess that makes sense. Using Reflector, I see that System.Runtime.Serialization also has to ascend the inheritance tree to get the list of serializable members, so I will, too. Thanks, Arun