Components
和WindowsFormsApplication5
的项目(还有更多,但这些是无关紧要的)。我所指的字典在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>>();
....
}
在
WindowsFormsApplication5
的Form1.cs
中(我没有选择这个名字,如果听起来乏味,请原谅),我正在使用我提到的属性。没有命名空间问题,并且编译正常进行。然而,在
Form1
的Form_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:我通过以下方式解决了这个问题:
- 应用了Matthew Watson的非序列化修复 - 以防编译器再次尝试序列化字典属性。
- 我注释掉了
Form1.Designer.cs
中上面提到的条目。
我接受Matthew的答案,因为我认为这个非序列化修复是相关的。