C# 如何将结构数组添加到字典中,
我已经创建了结构元素数组,必须将其添加到字典中。我的代码如下所示:C# 如何将结构数组添加到字典中,,c#,.net,exception-handling,dictionary,C#,.net,Exception Handling,Dictionary,我已经创建了结构元素数组,必须将其添加到字典中。我的代码如下所示: struct answerDetails { public string qId; public string question; public string answer; public string hint; } private answerDetails[] answers; private Dictionary<string, answerDetails[]> stu
struct answerDetails
{
public string qId;
public string question;
public string answer;
public string hint;
}
private answerDetails[] answers;
private Dictionary<string, answerDetails[]> studList = new Dictionary<string, answerDetails[]>();
foreach (var data in dynObj.Success)
{
foreach (var student in data.Answers)
{
answers = new answerDetails[student.Ques_Ans.Count];
int i = 0;
foreach (var qInfo in student.Ques_Ans)
{
answers[i].qId = qInfo.qId;
answers[i].question = qInfo.question;
answers[i].answer = qInfo.answer;
answers[i].hint = qInfo.hint;
i++;
}
studList.Add(student.studentId,answers);//raising error...
}
}
struct answerDetails
{
公共字符串qId;
公共字符串问题;
公共字符串应答;
公共字符串提示;
}
私人回答详细信息[]答案;
私有字典studList=新字典();
foreach(dynObj.Success中的var数据)
{
foreach(数据中的var学生答案)
{
答案=新答案详细信息[学生问题统计];
int i=0;
foreach(学生问题中的var qInfo)
{
答案[i].qId=qInfo.qId;
答[i].问题=qInfo.question;
答案[i].答案=qInfo.answer;
答案[i].hint=qInfo.hint;
i++;
}
studList.Add(student.studentId,answers);//引发错误。。。
}
}
但是,当我将struct数组添加到字典中时,它会生成
RuntimeBinderException
如果此行是RuntimeBinderException,您当前的动态student可能没有任何studentId属性,或者该属性可能不可见您使用数组而不是List
?这应该更容易使用。你选择使用结构而不是创建类有什么原因吗?@我必须处理多个数据类型。所以我选择了结构…我只是在字符串中给出了所有类型…但它也会有其他类型…你能用真实的类型替换var
?由于您使用动态类型,因此引发了RuntimeBinderException
,您确定它是在studList中引发的。添加(…)
行吗?您能否提供dynObj
和数据的类型?您的代码甚至不编译。