簡體   English   中英

如何獲取C#類的內容並放入/取出字符串?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM