My highlights.
- I use small names for the class and variable, or I use the command [ XmlAttribute ("ME")]
- I try not to post default values especially if they are string and large.
- I use [NonSerialized] for variables that I did not win for storage.
I also know that if I use an extra list inside my base class, they take up much more space. Here is an example that you can see for yourself, just playing with the moments that I mention.
For example, if you delete the default value from cEnaText, the view state is 50% less than with it. If you put [NonSerialized] on all variables, the viewstate will be empty. If you make a larger name, the viewstate will be larger.
[Serializable]
public class MyInts
{
public string cEnaText = "Start up text";
[XmlAttribute("TX")]
string inside_cEnaTextWorkAroundSolution;
public string cEnaTextWorkAroundSolution;
{
get
{
if(string.IsNullOrWhiteSpace(inside_cEnaTextWorkAroundSolution))
return "This is my default string that I do not won to save";
else
return inside_cEnaTextWorkAroundSolution;
}
set {inside_cEnaTextWorkAroundSolution = value;}
}
public int MyInt;
public MyInts(int getInt)
{
MyInt = getInt;
}
}
[Serializable]
public class StoreMeAsTest
{
public List<MyInts> MyL = new List<MyInts>();
public double cOneMoreVariable;
[XmlAttribute("ME")]
public double cOneMoreVariableEvenBigger;
[XmlIgnoreAttribute]
public List<MyInts> Temporary = new List<MyInts>();
public StoreMeAsTest()
{
for (int i = 0; i < 100; i++)
{
MyL.Add(new MyInts(i));
Temporary.Add(new MyInts(i));
}
}
}
public partial class Dokimes_Programming_Performance_ViewStatePerformance : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StoreMeAsTest StoreMe = new StoreMeAsTest();
ViewState["MyTestTable"] = StoreMe;
}
}
How to find out what is actually stored
There are several programs that you can read in view mode, this is what I found on Google.
http://www.binaryfortress.com/aspnet-viewstate-helper/
.
, , , , , . .
public static string ObjectToXML(Type type, object obby)
{
XmlSerializer ser = new XmlSerializer(type);
using (System.IO.MemoryStream stm = new System.IO.MemoryStream())
{
ser.Serialize(stm, obby);
stm.Position = 0;
using (System.IO.StreamReader stmReader = new System.IO.StreamReader(stm))
{
string xmlData = stmReader.ReadToEnd();
return xmlData;
}
}
}
MyLiteral.Text = ObjectToXML(typeof(StoreMeAsTest), StoreMe);
, . , , , .