0
我很努力地獲得能夠在嵌套類上設置值的「對象」,並且我懷疑我會在處理它之後如何處理它已設置。例子如下:使用反射嵌套靜態類的FieldInfo.SetValue
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";
}
}
下面的代碼使用反射來獲得數據字段,但是也看不出如何獲取「對象」通入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 the Data field
// Issue 1: how to allocate the Data Field within the nested class
field.SetValue(??? object ??? , new RegistryData()); <---
// Issue 2: how to access the new class within the nested class
field.GetValue(??? what ???).rk = Registry.LocalMachine;
}
}
謝謝。