序列化对象 [英] Serializing an object

查看:86
本文介绍了序列化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected override void LoadToView(CaseStageDomObj caseStage)
        {
                Button btn = (Button)((ViewComplaint)Page).ReturnTheButton();
                Button _button = (Button)Serialize<Button>(btn);
                ViewState.Add("Button", _button);
        }

public static MemoryStream Serialize<Button>(Button btn)
        {
            
            DataContractSerializer s = new DataContractSerializer(typeof(Button));
            MemoryStream ms = new MemoryStream();
            s.WriteObject(ms, btn);
            return ms;
        } 



为什么显示以下错误

无法将类型''System.IO.MemoryStream''转换为''System.Web.UI.WebControls.Button''



Why its showing the following error

Cannot convert type ''System.IO.MemoryStream'' to ''System.Web.UI.WebControls.Button''

推荐答案

您的调用返回:Serialize< button>(btn):是System.IO.MemoryStream对象保留了Button的序列化形式.

您不能只是将其投射"回Button对象本身.

您必须反序列化MemoryStream.您可以使用与序列化相同的DataContractSerializer,但用MS表示:要检索数据,请创建一个适合于读取数据格式的对象(例如XML文档的XmlDictionaryReader)并调用ReadObject方法."

最好,比尔
The return of your call to: Serialize<button>(btn): is a System.IO.MemoryStream object that holds the serialized form of the Button.

You can''t just "cast" that back to the Button object itself.

You have to de-serialize the MemoryStream. You can use the same DataContractSerializer you use to serialize, but, in MS'' words: "To retrieve data, create an object appropriate to reading the data format (such as an XmlDictionaryReader for an XML document) and call the ReadObject method."

best, Bill


这篇关于序列化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆