ViewState对象序列化

 
protected void Page_Load(object sender, EventArgs e)
        {
            Demo d = new Demo();
            d.Name = "Wing";
            d.Age = 20;
            
            ViewState["key"] = d;

            Response.Write(((Demo)ViewState["key"]).Name + "<br>");
            Response.Write(((Demo)ViewState["key"]).Age + "<br>");
        }

        [Serializable]
        public class Demo
        {
            private string _Name;

            public string Name
            {
                get { return _Name; }
                set { _Name = value; }
            }
            private int _Age;

            public int Age
            {
                get { return _Age; }
                set { _Age = value; }
            }
        }
亮点是在要序列化的类上加[Serializable]标记即可。

你可能感兴趣的:(String,object,Class)