[英]How can I take the contents of a C# class and put into / take out of a string?
我有以下两节课:
public class TestQuestionModel
{
public TestQuestionModel()
{
this.Answers = new List<TestQuestionAnswerModel>();
}
public int TestId { get; set; }
public int TestQuestionId { get; set; }
public int QuestionNumber { get; set; }
public virtual ICollection<TestQuestionAnswerModel> Answers { get; set; }
}
public class TestQuestionAnswerModel
{
public int AnswerUId { get; set; }
public bool? Correct { get; set; }
public bool? Response { get; set; }
public string Text { get; set; }
}
我想做的是将答案存储在字符串中并放入此类:
public class TestQuestion
{
public int TestId { get; set; }
public int TestQuestionId { get; set; }
public int QuestionNumber { get; set; }
public string AnswerString { get; set; }
}
有人可以告诉我如何双向执行此操作(将其放入字符串中并取出)
可能的方式:
public string Serialize(ICollection<TestQuestionAnswerModel> answers)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(answers);
}
public ICollection<TestQuestionAnswerModel> Deserialize(string data)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return (ICollection<TestQuestionAnswerModel>)serializer.Deserialize<ICollection<TestQuestionAnswerModel>>(data);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.