我有一个我编辑过的网络服务,在它运行之前没有问题。但是现在我得到这个错误:无法序列化,因为它没有无参数构造函数我已经在下面发布了我的类。
public class Class
{
public class AnsweredQ
{
public string Question { get; set; }
public string Answer { get; set; }
public AnsweredQ(string _Question, string _Answer)
{
Question = _Question;
Answer = _Answer;
}
}
public class UnAnsweredQ
{
public string Question { get; set; }
public string[] Options { get; set; }
public UnAnsweredQ(string _Question, string[] _Options)
{
Question = _Question;
Options = _Options;
}
}
public class Trial
{
public string User { get; set; }
public DateTime TrialDate { get; set; }
public bool Expired { get; set; }
public Trial (string _User, DateTime _TrialDate, bool _Expired)
{
User = _User;
TrialDate = _TrialDate;
Expired = _Expired;
}
}
}
如果你能解决这个问题,请解释我做错了什么:)
答案 0 :(得分:9)
为了能够序列化/反序列化类,序列化器需要一个无参数构造函数。因此,您需要将无参数构造函数添加到类中,即:
public class AnsweredQ
{
public string Question { get; set; }
public string Answer { get; set; }
public AnsweredQ() { }
public AnsweredQ(string _Question, string _Answer)
{
Question = _Question;
Answer = _Answer;
}
}
public class UnAnsweredQ
{
public string Question { get; set; }
public string[] Options { get; set; }
public UnAnsweredQ() {}
public UnAnsweredQ(string _Question, string[] _Options)
{
Question = _Question;
Options = _Options;
}
}
public class Trial
{
public string User { get; set; }
public DateTime TrialDate { get; set; }
public bool Expired { get; set; }
public Trial ()
{
}
public Trial (string _User, DateTime _TrialDate, bool _Expired)
{
User = _User;
TrialDate = _TrialDate;
Expired = _Expired;
}
}
}