0

所以我正在开发一个现有的应用程序,它有一个很好的小助手方法,最终调用 ObjectStateFormatter Serialize() - 这是代码。

/// <summary>
/// Transforms a limited object serialization (LOS)-formatted object into a view-state value and places the results into a <see cref="T:System.IO.TextWriter"/> object.
/// </summary>
/// <param name="output">The <see cref="T:System.IO.TextWriter"/> to receive the transformed value. </param><param name="value">The LOS-formatted object to transform into a view-state value. </param>
public void Serialize(TextWriter output, object value)
{
  this.SerializeInternal(output, value);
}

private void SerializeInternal(TextWriter output, object value)
{
  string str = this._formatter.Serialize(value);
  output.Write(str);
}

我是否正确地假设它忽略了我的 List 属性?

谢谢肯

4

1 回答 1

0

如果有人想知道 - LosFormatter 确实会序列化一个通用列表。我只是做了一个小测试。这当然让我弄清楚我刚刚继承的架构中的哪个地方失败了,但至少我知道它会起作用。这是我放在一起的示例。

public ActionResult Index()
{
    ViewData["Message"] = "Welcome to ASP.NET MVC!";
    var Person = new Person
                     {
                         FirstName = "Ken",
                         LastName = "N",
                         Phones = new List<Phone>
                                      {
                                          new Phone {Ext = "O", PhoneNumber = "2341234"},
                                          new Phone {Ext = "O", PhoneNumber = "2341234"},
                                          new Phone {Ext = "O", PhoneNumber = "2341234"}
                                      }
                     };
    var serializedPersonObject = LosSerializeObject(Person);
    ViewData["PersonObject"] = serializedPersonObject;
    return View();
}

public ActionResult About()
{
    var personString = Request.QueryString[0];
    var person = (Person) RetrieveObjectFromViewState(personString);
    return View();
}

string LosSerializeObject(object obj)
{
    var los = new System.Web.UI.LosFormatter();
    var writer = new StringWriter();
    los.Serialize(writer, obj);
    return writer.ToString();
}

object RetrieveObjectFromViewState(string serializedObject)
{
    var los = new System.Web.UI.LosFormatter();
    return los.Deserialize(serializedObject);
}

然后是 Index.ASPX 页面上的一个按钮

<%= Html.ActionLink("Check this", "About", "Home", new { person = ViewData["PersonObject"] }, null)%>

果然,对序列化和反序列化的调用给了我电话列表。

于 2012-08-28T02:10:20.787 回答