GetValue on static field inside nested classes.
Posted
by Sir Gallahad
on Stack Overflow
See other posts from Stack Overflow
or by Sir Gallahad
Published on 2010-04-07T13:00:25Z
Indexed on
2010/04/07
13:03 UTC
Read the original article
Hit count: 198
Hi...
I have the following class declared. I need to retreive the class structure and the static values without instanciate it.
public MyClass()
{
public static string field = "Value";
public nestedClass()
{
public static string nestedField = "NestedValue";
}
}
I've successfuly used GetFields and GetNestedType to recover the class structure and GetValue(null) works fine on field, but not on nestedField. Let me sample:
var fi = typeof(MyClass).GetField("field", BindingFlags.Public | BindingFlags.Static);
var nt = typeof(MyClass).GetNestedType("nestedClass", BindingFlags.Public);
var nfi = nt.GetField("nestedField", BindingFlags.Public | BindingFlags.Static);
// All the above references are detected correctly
var value = fi.GetValue(null); // until here everything works fine. value == "Value"
var nestedValue = nfi.GetValue(null); // this one does not work!!
Anyone knows why the last line does not work and how to work around? Thanks.
© Stack Overflow or respective owner