asp.net - 如何优化 View 状态的类

标签 asp.net serialization viewstate binary-serialization

如果我有一个对象需要存储在 View 状态中,我可以做哪些事情来优化存储对象所需的大小?显然,存储最少的数据将占用更少的空间,但除此之外,有没有办法构建类、属性、属性等,这会影响序列化输出的大小?

最佳答案

我的基本观点。

  • 我对类和变量使用小名称,或者我使用 [ XmlAttribute ("ME")] 命令
  • 如果是字符串和大的,我尽量不放置默认值。
  • 我使用 [非序列化] 对于我不想存储的变量。

  • 我也知道,如果我在基类中使用额外的 List,它们会占用更多空间。这是一个示例,您可以通过使用我提到的要点自行查看。

    例如,如果您从 cEnaText 中删除默认值,则 View 状态将比它小 50%。如果将 [NonSerialized] 放在所有变量上,则 View 状态为空。如果你把名字变大,那么viewstate就会变大。
    [Serializable]
    public class MyInts
    {
        // this text will stored even if you never used it, Avoid to setup it here.
        public string cEnaText = "Start up text";    
    
        // a work around for big names, and default text.
        [XmlAttribute("TX")]
        string inside_cEnaTextWorkAroundSolution;    
    
        // this is not going to saved on xml.
        public string cEnaTextWorkAroundSolution;    
        {
           get
           {
             // here I return a default text that I do not store on xml
             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;}
        } 
    
    
        // this is stored, including the class name
        public int MyInt;
    
        // this is not stored
        public MyInts(int getInt)
        {
            MyInt = getInt;
        }
    }
    
    [Serializable]
    public class StoreMeAsTest
    {
        // this is stored
        public List<MyInts> MyL = new List<MyInts>();
    
        // keep the name small (not like this one)
        public double cOneMoreVariable;
    
        // or change the xml attribute name with this command
        [XmlAttribute("ME")]
        public double cOneMoreVariableEvenBigger;
    
        // this is not stored
        [XmlIgnoreAttribute]
        public List<MyInts> Temporary = new List<MyInts>();
    
    
        // this is not stored
        public StoreMeAsTest()
        {
            // create some test data
            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;
        }
    }
    

    如何查看实际存储的内容

    有一些程序可以读取 View 状态,这是我在 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);
    

    最后的话

    您实际上在这里询问我们可以优化对象及其非常好的问题。下一步可能是压缩 View 状态,使其在传回给我们时变小。

    关于asp.net - 如何优化 View 状态的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2784132/

    相关文章:

    c# - ASP.NET MVC如何从应用程序中删除ViewState?

    javascript - 在一个列表框中选择多个术语 asp c#

    asp.net - ASP.NET 应用程序的国际化

    c# - 接口(interface)中的静态

    c# - 无法加载 View 状态

    c# - 使用 cookie 和 viewState 将数据发布到网站

    javascript - 改变 IFrame src 内容

    c# - 将 ViewModel 与包含 HttpPostedFileBase 的 ViewModel 列表绑定(bind)到 Controller 操作

    go - Go 中的 Java DataOutputStream 替代方案

    java - 序列化 Path2D.Double 的子类时为 "no valid constructor"