Viewstate Serialization Issue [modified]
-
Hi, I have a class marked as [Serializable] named "TestClass". When i store the object of this class Why the [NonSerialized()] attribute fname successfully serialized and deserialized back when i create the object from viewstate? Following is the full code of my test. using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Runtime.Serialization; using System.Xml.Serialization; public partial class viewstate : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { TestClass obj = new TestClass(); obj.name = "Dreams"; obj.fname = "Lonely"; ViewState["MyDT"] = obj; } protected void Button1_Click(object sender, EventArgs e) { TestClass obj = (TestClass)ViewState["MyDT"]; Response.Write("id=" + obj.j.ToString() + " Name=" + obj.name.ToString() + " Age=" + obj.age.ToString() + " fname="+ obj.fname +""); } } [Serializable] public class TestClass { public int j = 1; public string name = "Dreams"; public string age = "28"; [NonSerialized()] public string fname = "Planet"; } Thanks
-
Hi, I have a class marked as [Serializable] named "TestClass". When i store the object of this class Why the [NonSerialized()] attribute fname successfully serialized and deserialized back when i create the object from viewstate? Following is the full code of my test. using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Runtime.Serialization; using System.Xml.Serialization; public partial class viewstate : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { TestClass obj = new TestClass(); obj.name = "Dreams"; obj.fname = "Lonely"; ViewState["MyDT"] = obj; } protected void Button1_Click(object sender, EventArgs e) { TestClass obj = (TestClass)ViewState["MyDT"]; Response.Write("id=" + obj.j.ToString() + " Name=" + obj.name.ToString() + " Age=" + obj.age.ToString() + " fname="+ obj.fname +""); } } [Serializable] public class TestClass { public int j = 1; public string name = "Dreams"; public string age = "28"; [NonSerialized()] public string fname = "Planet"; } Thanks
Hi, First you need to Serialize the TestClass object. Here is how to do it. Serialization TestClass obj = new TestClass(); obj.name = "Dreams"; obj.fname = "Lonely"; FileStream fs = new FileStream("SerializedString.Data", FileMode.Create); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, obj); fs.Close(); Then you need to deserialize it like this. Here is the code for that. Deserialization FileStream fs = new FileStream("SerializedString.Data", FileMode.Open); BinaryFormatter bf = new BinaryFormatter(); TestClass obj= (TestClass)bf.Deserialize(fs); fs.Close(); Response.Write("id=" + obj.j.ToString() + " Name=" + obj.name.ToString() + " Age=" + obj.age.ToString() + " fname="+ obj.fname +""); Hope it helped. Thx, Gayani
-
Hi, First you need to Serialize the TestClass object. Here is how to do it. Serialization TestClass obj = new TestClass(); obj.name = "Dreams"; obj.fname = "Lonely"; FileStream fs = new FileStream("SerializedString.Data", FileMode.Create); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, obj); fs.Close(); Then you need to deserialize it like this. Here is the code for that. Deserialization FileStream fs = new FileStream("SerializedString.Data", FileMode.Open); BinaryFormatter bf = new BinaryFormatter(); TestClass obj= (TestClass)bf.Deserialize(fs); fs.Close(); Response.Write("id=" + obj.j.ToString() + " Name=" + obj.name.ToString() + " Age=" + obj.age.ToString() + " fname="+ obj.fname +""); Hope it helped. Thx, Gayani
I agreed. But storing object into the viewstate is also the process of serialization. If i removed the [Serializable] attribute from TestClass the application won't work, because to store the object, class must have that attribute. Than why that process won't understand [NonSerialized()] attribute? Is viewstate serialization is different form of process? Thanks,