尽管已经实例化了字典,但字典属性始终为空

3
首先,我的解决方案中有两个名为ComponentsWindowsFormsApplication5的项目(还有更多,但这些是无关紧要的)。我所指的字典在Components中,定义如下,以及它们的属性 -:
 Dictionary<int, ButtonBase> RespMCQ, DispMCQ; //<Question Number, Checkbox/Radiobutton>
 Dictionary<int, List<ButtonBase>> RespMSQ, DispMSQ;
 public Dictionary<int, ButtonBase> MCQResp
 {
     get
     {
         return RespMCQ;
     }
     set
     {
         RespMCQ = value;
     }
 }
 public Dictionary<int, List<ButtonBase>> MSQResponse
 {
     get
     {
         return RespMSQ;
     }
     set
     {
         RespMSQ = value;
     }
 }
 public Dictionary<int, ButtonBase> MCQDisplay
 {
     get
     {
         return DispMCQ;
     }
     set
     {
         DispMCQ = value;
     }
 }
 public Dictionary<int, List<ButtonBase>> MSQDisplay
 {
     get
     {
         return DispMSQ;
     }
     set
     {
         DispMSQ = value;
     }
 }

字典在QuestionDisplay.cs的构造函数中实例化,该文件是Components项目的一部分(也是它们所在的地方)。具体如下 -:

public QuestionDisplay()
{
    InitializeComponent();
    RespMCQ = new Dictionary<int, ButtonBase>();
    DispMCQ = new Dictionary<int, ButtonBase>();
    RespMSQ = new Dictionary<int, List<ButtonBase>>();
    DispMSQ = new Dictionary<int, List<ButtonBase>>();
    ....
}

WindowsFormsApplication5Form1.cs中(我没有选择这个名字,如果听起来乏味,请原谅),我正在使用我提到的属性。没有命名空间问题,并且编译正常进行。
然而,在Form1Form_Load期间,当我引用这些属性时,它们抛出一个NullReferenceException,并显示以下内容:
Object reference not set to an instance of an object.

这些属性的第一次出现是在一个叫做WorkFlowPanel(string S, QuestionContainerState obj)的方法中,该方法在表单加载时被调用:
 if (QuesTypes[i].Equals("MCQ"))
 {
    string text = "";
    for (int ansCount = 0; ansCount < questions[i].Options.Count(); ansCount++)
    {
        if (Convert.ToInt32(K.Key).Equals(ansCount + 1))
        {
            text = questions[i].Options[ansCount];
            break;
        }
    }
    questionDisplay1.MCQResp.Add(i, new CustomRadio { optionId = Convert.ToInt32(K.Key), Checked = true, Text = text }); //Using anonymous class
 }
 else if (QuesTypes[i].Equals("MSQ") || QuesTypes[i].Equals("Linked"))
 {
     var storeoptions = K.Key.Split(':');
     questionDisplay1.MSQResponse.Add(i, new List<ButtonBase>());
     for (int s = 0; s < storeoptions.Count(); s++)
     {
         questionDisplay1.MSQResponse[i].Add(new CustomChecks { optionId = Convert.ToInt32(storeoptions[s]), Checked = true, Text = questions[i].Options[Convert.ToInt32(storeoptions[s])] });
     }
 }

令我惊讶的是,在调试过程中,尽管字典已被实例化,但属性始终为空。现在这个问题有一点历史需要探究以提供背景。
我之前正在处理一个“找不到名称类型。该类型为System.Collections.Dictionary...”的错误,它发生在Form1.resx中(我无法回忆整个错误)。 显然,字典属性被序列化为二进制格式,并以base64编码方式编码到resx文件中,如下所示:
<data name="MCQDisplay" mimetype="application/x-microsoft.net.object.binary.base64">
<value>
        AAEAAAD/////AQAAAAAAAAAEAQAA....
</value>
<data name="MSQDisplay" .....>
....
....

我按照Stackoverflow上找到的解决方案,将代码复制回旧版本中。从那时起,这就是我一直在应用的修复方法。我发现,如果我删除这些条目(编译器错误提示我这么做),字典属性总是为空。我觉得这与我的当前情况有关。恰好在复制回操作期间,resx文件条目不知何故消失了。

自那以后,它再也没有抛出过这个编译器错误。我尝试了所有方法,包括使用ResXResourceWriter重新生成条目。就像它们根本没有被承认一样,而且在运行时,我被告知字典属性始终为空,而不是错误! IRC上的一个人建议,错误之所以会发生,是因为ButtonBase不能序列化,即使Dictionary本身可以。好吧,我从来没有让编译器序列化字典。我想知道为什么会发生这种情况。

如果有人能帮助我解决这个致命的错误,我会非常感激。我还有一个模糊的解决方案,即不是将ButtonBase子项存储为值,而是存储类对象,这些类对象具有与ButtonBase子项的文本、选项ID和选中属性相对应的属性。然而,我认为这是最后的办法。

EDIT_1:我不知道这是否相关,但我在Form1.Designer.cs中找到了这些条目:

this.questionDisplay1.MCQDisplay = ((System.Collections.Generic.Dictionary<int, System.Windows.Forms.ButtonBase>)(resources.GetObject("questionDisplay1.MCQDisplay")));
this.questionDisplay1.MCQResp = ((System.Collections.Generic.Dictionary<int, System.Windows.Forms.ButtonBase>)(resources.GetObject("questionDisplay1.MCQResp")));
this.questionDisplay1.MSQDisplay = ((System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<System.Windows.Forms.ButtonBase>>)(resources.GetObject("questionDisplay1.MSQDisplay")));
this.questionDisplay1.MSQResponse = ((System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<System.Windows.Forms.ButtonBase>>)(resources.GetObject("questionDisplay1.MSQResponse")));

我需要移除这些东西或进行某种修改吗?我感觉它们与Matthew的回答有关。

编辑_2:我通过以下方式解决了这个问题:

  1. 应用了Matthew Watson的非序列化修复 - 以防编译器再次尝试序列化字典属性。
  2. 我注释掉了Form1.Designer.cs中上面提到的条目。

我接受Matthew的答案,因为我认为这个非序列化修复是相关的。


如果您展示一下 Components 类中构造函数的代码,以及如何实例化该类的实例以及如何尝试访问字典,那将会很有帮助。 - Steve
@Steve:立即添加! - PritishC
1个回答

1
这些属性是公共的并且属于从FormUserControlControl派生的类吗?
如果是,窗体设计器将尝试将这些属性序列化到窗体的Designer.cs文件中。 (事实上,根据你所说的,这是完全可能的。)
为了防止这种情况发生,您可以使用DesignerSerializationVisibilityAttribute来防止它,例如:
[
    Browsable(false),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]

public MyType MyPropertyWhichMustNotBeSerialized
{
    get;
    set;
}

您需要编辑掉已经放在".Designer.cs"文件中的任何错误内容 - 添加此属性可能不会自动使其消失。

如果您没有定期检查是否需要添加此属性,我认为您可能需要检查所有从Control直接或间接派生的类中的所有公共属性,并在必要时添加此属性。


对于空引用问题,FormLoad()是否可能作为调用InitialiseComponent()的副作用而被调用?这将导致空引用错误。
为了更安全,您应该使用支持字段来替代自动属性。
然后,不要在构造函数中初始化字段,而是在声明字段的地方进行初始化,像这样:
private Dictionary<int, ButtonBase>       respMCQ = new Dictionary<int, ButtonBase>();
private Dictionary<int, ButtonBase>       dispMCQ = new Dictionary<int, ButtonBase>();
private Dictionary<int, List<ButtonBase>> respMSQ = new Dictionary<int, List<ButtonBase>>();
private Dictionary<int, List<ButtonBase>> dispMSQ = new Dictionary<int, List<ButtonBase>>();

这样做可以确保在调用任何构造函数之前初始化它们。


这些属性确实是公共的,但它们所在的类派生自“UserControl”。不过我仍然会尝试您的修复方法。首先将清理resx文件中存在的条目。 - PritishC
@UncleDolan 我也应该说“UserControl” - 它也受到影响。 - Matthew Watson
尝试了这个,但是没有任何不同。空引用异常仍然被抛出,并且它发生在我尝试向questionDisplay1.MSQResponse添加条目的那一行。 - PritishC
1
@UncleDolan 我怀疑有不止一个 bug,而这只是修复其中一个。 - Matthew Watson
我认为在将这些实例化移动到构造函数之前,我应该补充一下。最初,我是在声明时进行实例化的。但是当我进行了这个更改后,并没有发生任何变化,异常仍然被抛出。 - PritishC
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接