13

In C#, when I'm reflecting over a derived type, how come I don't see base classes' static fields?

I've tried both type.GetFields(BindingFlags.Static) and type.GetFields().

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
jameszhao00
  • 7,213
  • 15
  • 62
  • 112

4 Answers4

21

This is how it works. static members are really non-object-oriented stuff. They are not polymorphic and they really belong to their declaring type and are unrelated to other types in the inheritance hierarchy. For instance, a static initializer for a base class is not required to run before accessing a static member in a derived class.

static members of base classes are not returned unless BindingFlags.FlattenHierarchy is specified:

type.GetFields(BindingFlags.Static 
             | BindingFlags.FlattenHierarchy
             | BindingFlags.Public)
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • Should be noted that this still won't find *private* statics on a base class. Just in case that's what OP is trying to do. – womp Aug 24 '09 at 23:31
  • Yes, that was just an example. Just add `BindingFlags.NonPublic` in case you need it. – Mehrdad Afshari Aug 24 '09 at 23:32
  • 2
    That still won't do it actually. You have to reflect on private static fields directly on the base class. Although I hope OP isn't trying to do that ;) – womp Aug 24 '09 at 23:34
  • Quick question though. I tried BindingFlags.FlattenHierarchy stuff to find methods and in this case it returns nothing. Why is that so? – jameszhao00 Aug 24 '09 at 23:44
  • You'll have to specify `BindingFlags.Public` too. Are there any public methods? – Mehrdad Afshari Aug 24 '09 at 23:45
  • Yea they are all public. I specified BindingFlags.Public|BindingFlags.FlattenHierarchy and nothing came up. Just a bit weird. If I specify no flags all public base/derived methods come up. – jameszhao00 Aug 24 '09 at 23:47
  • 1
    You need to specify `BindingFlags.Static` and/or `BindingFlags.Instance`. – Mehrdad Afshari Aug 24 '09 at 23:52
  • I struggled with this a bit untill I realized that what I was trying to look up was a property... – Tor Thorbergsen Mar 18 '20 at 13:35
6

Because they belong to the base type, and are not inherited. Move up to that type, and you'll find them.

-- Edit

Mehrdad has the correct answer, but just for completeness:

foreach(FieldInfo f in b.GetType().GetFields(
    BindingFlags.Static
    | BindingFlags.FlattenHierarchy
    | BindingFlags.Instance
    | BindingFlags.Public
    )){
    Console.WriteLine("found: " + f.Name);
}
Noon Silk
  • 54,084
  • 6
  • 88
  • 105
5

Set the BindingFlags.FlattenHierarchy enumeration to Static and this will also search static members. More information: http://msdn.microsoft.com/en-us/library/system.reflection.bindingflags.aspx

choudeshell
  • 514
  • 3
  • 10
3

Your type is just your type - it doesn't include base types. You'll need to use BindingFlags.FlattenHierarchy if you want to search fields in base classes.

You should probably take a look at the BindingFlags documentation to accomplish what you need.

womp
  • 115,835
  • 26
  • 236
  • 269