Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用反射保存Winform控件状态,如复选框c#.net_C#_.net_Reflection_Field - Fatal编程技术网

使用反射保存Winform控件状态,如复选框c#.net

使用反射保存Winform控件状态,如复选框c#.net,c#,.net,reflection,field,C#,.net,Reflection,Field,我正试图使用反射来保存winform上控件的状态,但在info=myType.GetField上得到一个空引用。。。线 这是一个保存函数,它通过将列表保存为字符串来工作。 例如,cbSetMyParameter,false,cbsetother,true private void save_states() { c = GetAll(this, typeof(CheckBox)); string[] splits = ""; foreach (CheckBox cb in

我正试图使用反射来保存winform上控件的状态,但在info=myType.GetField上得到一个空引用。。。线

这是一个保存函数,它通过将列表保存为字符串来工作。 例如,cbSetMyParameter,false,cbsetother,true

private void save_states()
{
    c = GetAll(this, typeof(CheckBox));
    string[] splits = "";
    foreach (CheckBox cb in c) { splits += cb.Name + ',' + cb.Checked.ToString() + ','; }
    Properties.Settings.Default.CheckBoxStates = splits;
}
然后我解析字符串并希望使用引用来设置复选框, 但是当我尝试使用反射来检索对控件的引用时,我得到了一个空值

private void get_state()
{
    Type myType = this.GetType();
    splits = Properties.Settings.Default.CheckBoxStates.Split(',');
    for (cnt=0; cnt < splits.Count()/2; cnt += 2)
    {
        // Get Method Information.
        FieldInfo info = myType.GetField(splits[cnt], BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetField ); 
        CheckBox cb = (CheckBox)info.GetValue(this);
    }
}
private void get_state()
{
类型myType=this.GetType();
splits=Properties.Settings.Default.CheckBoxStates.Split(',');
对于(cnt=0;cnt
看起来像是
分裂[cnt]
是根本原因。您需要调试并检查
splits[cnt]
在每次迭代期间是否实际有任何值。在大多数情况下,您应该避免反射。最有可能的是,你的设计就是问题所在,把注意力放在这个问题上,而不是用反射来解决糟糕的设计。拆分[cnt]不是问题所在。我已经仔细检查了返回的字符串是否正确。