ObjectStateFormatter.Serialize似乎忽略了我的数组:(

时间:2012-08-27 17:51:30

标签: c# asp.net-mvc

所以我正在开发一个现有的应用程序,它有一个很好的小助手方法,最后调用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?

由于 肯

1 个答案:

答案 0 :(得分: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)%>

果然,对Serialize和Deserialize的调用给了我电话列表。