惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

博客园 - shcity

Troubleshooting on TransactionScope Parse string to JSON object Parse date in js blockUI doesn't close when download file in asp.net define namespace in JS Dense_Rank(), Row_Number(), Rank() in sql server reading and writing variable through lock in SSIS script task Fix the issue that cannot open SSIS in BIDS three ways creating custom helpers to show RadioButtonList in MVC using JavaScriptSerializer to serialize object to json using ISerializable to control serialization and deserialization ReportingService formatting Build my own DataTable split a string into an array through comma div with separated html template - shcity 正则表达式替换日期 半透明的div对话框 foreach 的自动转化类型 在Ajax1.0中调用页面CS文件中的方法
ViewStateAutoManager
shcity · 2011-09-20 · via 博客园 - shcity

using System;
using System.Web.UI;
using System.Reflection;
using System.Collections.Generic;
using System.Collections;

namespace My.Common
{
    [AttributeUsage(AttributeTargets.Property)]
    public class ViewStateAttribute : Attribute
    {
    }

    public class BasePage : Page
    {
        protected override object SaveViewState()
        {
            ViewStateAutoManager mgr = new ViewStateAutoManager(this);
            var v = base.SaveViewState();
            return mgr.SaveViewState(v);
        }

        protected override void LoadViewState(object savedState)
        {
            ViewStateAutoManager mgr = new ViewStateAutoManager(this);
            var v = mgr.LoadViewState(savedState);
            base.LoadViewState(v);
        }
    }

    public class BasePart : UserControl
    {
        protected override object SaveViewState()
        {
            ViewStateAutoManager mgr = new ViewStateAutoManager(this);
            var v = base.SaveViewState();
            return mgr.SaveViewState(v);
        }

        protected override void LoadViewState(object savedState)
        {
            ViewStateAutoManager mgr = new ViewStateAutoManager(this);
            var v = mgr.LoadViewState(savedState);
            base.LoadViewState(v);
        }
    }

    public class ViewStateAutoManager
    {
        private static Dictionary<Type, List<PropertyInfo>> _cache = new Dictionary<Type, List<PropertyInfo>>();
        private Control _ctl;

        public ViewStateAutoManager(Control ctl)
        {
            this._ctl = ctl;
        }

        private List<PropertyInfo> GetViewStateProperties(Control ctl)
        {
            var targetType = ctl.GetType();

            if (!_cache.ContainsKey(targetType))
            {
                var pros = targetType.GetProperties(
                    BindingFlags.Public
                    | BindingFlags.NonPublic
                    | BindingFlags.Instance
                    );

                var list = new List<PropertyInfo>();

                foreach (var p in pros)
                {
                    var attr = Attribute.GetCustomAttribute(p, typeof(ViewStateAttribute));
                    if (attr != null)
                    {
                        list.Add(p);
                    }
                }

                _cache[targetType] = list;
            }

            return _cache[targetType];
        }

        private ViewStateList GetViewState()
        {
            var data = new ViewStateList();
            var pros = GetViewStateProperties(_ctl);

            foreach (var p in pros)
            {
                var value = p.GetValue(_ctl, null);
                data.Add(p.Name, value);
            }

            return data;
        }

        public object SaveViewState(object parentViewState)
        {
            var my = GetViewState();
            return new Pair(parentViewState, my);
        }

        private void SetViewState(object viewState)
        {
            var data = viewState as ViewStateList;
            var pros = GetViewStateProperties(_ctl);

            foreach (var p in pros)
            {
                p.SetValue(_ctl, data[p.Name], null);
            }
        }

        public object LoadViewState(object parentViewState)
        {
            var pair = parentViewState as Pair;
            SetViewState(pair.Second);
            return pair.First;
        }
    }

    [Serializable]
    public class ViewStateList : IEnumerable<ViewStateList._Item>
    {
        [Serializable]
        public class _Item
        {
            public string Key;
            public object Value;
            public _Item Next;
        }

        private _Item First;

        public void Add(string key, object value)
        {
            var newItem = new _Item { Key = key, Value = value };

            if (First == null)
            {
                First = newItem;
            }
            else
            {
                _Item last = First;
                while (last.Next != null)
                {
                    last = last.Next;
                }
                last.Next = newItem;
            }
        }

        public object this[string key]
        {
            get
            {
                foreach (var p in this)
                {
                    if (p.Key == key)
                    {
                        return p.Value;
                    }
                }

                return string.Empty;
            }
        }

        #region IEnumerable<_Item> Members

        public IEnumerator<ViewStateList._Item> GetEnumerator()
        {
            for (var p = First; p != null; p = p.Next)
            {
                yield return p;
            }
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion
    }
}