This is what I've done so far:
var fields = typeof (Settings.Lookup).GetFields(); Console.WriteLine(fields[0].GetValue(Settings.Lookup)); // Compile error, Class Name is not valid at this point
And this is my static class:
public static class Settings { public static class Lookup { public static string F1 ="abc"; } }
Static variables can be accessed by calling with the class name ClassName. VariableName. When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final, the naming syntax is the same as instance and local variables.
Public static fields are useful when you want a field to exist only once per class, not on every class instance you create. This is useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.
Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory.
You need to pass null
to GetValue
, since this field doesn't belong to any instance:
fields[0].GetValue(null)
You need to use Type.GetField(System.Reflection.BindingFlags) overload:
For example:
FieldInfo field = typeof(Settings.Lookup).GetField("Lookup", BindingFlags.Public | BindingFlags.Static); Settings.Lookup lookup = (Settings.Lookup)field.GetValue(null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With