c# - FieldInfo.SetValue on a Nested Static Class using Reflection -
i struggling obtaining "object" able set value on nested class, , suspect going struggle how manipulate after set. example follows:
public class registrydata {     public registrykey rk; }  public static class registrykeys {     public static class username {         public static registrydata data = null;         public static string defaultvalue = "myusername";     }     public static class password {         public static registrydata data = null;         public static string defaultvalue = "mypassword";     } }   the following code uses reflection obtain data field, can not see how obtain "object" pass fieldinfo.setvalue().
static void doreflection() {     type type = typeof(registrykeys);     type[] nestedtypearray = type.getnestedtypes(bindingflags.public | bindingflags.static);     foreach(type t in nestedtypearray)     {         fieldinfo field = t.getfield("data"); // obtain data field                      // issue 1: how allocate data field within nested class         field.setvalue( ??? object ??? , new registrydata());  <---          // issue 2: how access new class within nested class         field.getvalue( ??? ??? ).rk = registry.localmachine;     } }   thanks.
ok, turns out little more research (asking question differently in google) , have found answer:
field.setvalue( null , new registrydata()); ((registrydata)(field.getvalue(null))).rk = registry.localmachine;   according answer here: is possible set static private member of static class reflection?
it states in code section:
// first argument "setvalue" instance // of type since mutating static field pass "null"   i not going suggest understand that. code working! rather delete question post answer here others did struggle find answer.
Comments
Post a Comment