A webservice cannot be serialized because it does not have a constructor without parameters

I have a webservice that I edited before it worked without problems. however, now I get this error: it cannot be serialized because it does not have a constructor without parameters. I posted my class below.

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; } } } 

if you can solve this, please explain what I did wrong :)

+7
source share
1 answer

To be able to serialize / deserialize a class, a serializer requires a parameterless constructor. So, you need to add constructors without parameters to your classes, i.e.:

 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; } } } 
+9
source

All Articles