加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何创建Generic StateManagedCollection?

发布时间:2020-12-16 04:28:39 所属栏目:asp.Net 来源:网络整理
导读:一个例子描述为 here.但是作者显然忘记包含下载代码. 另一个例子显示为here.但是,这个例子并不完全(如评论中所述). 你是怎么做到这一点的? 解决方法 DanHerbert得到了它. Darn,我也花了几个小时!在尝试回答这个问题的过程中,我提出了一个简化的通用StateMa
一个例子描述为 here.但是作者显然忘记包含下载代码.

另一个例子显示为here.但是,这个例子并不完全(如评论中所述).

你是怎么做到这一点的?

解决方法

DanHerbert得到了它. Darn,我也花了几个小时!在尝试回答这个问题的过程中,我提出了一个简化的通用StateManagedCollection,它继承自框架的内置StateManagedCollection,基于版本 here.也许你会发现它很有用.我的示例项目的完整源代码可用于 here.
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Security.Permissions;
using System.Web;
using System.Collections.Generic;
using System.Web.UI;

namespace Web
{
    public abstract class StateManagedCollection<T> : StateManagedCollection,IList<T>,ICollection<T>,IEnumerable<T>
        where T : class,IStateManagedItem,new()
    {

        protected override object CreateKnownType(int index)
        {
            return Activator.CreateInstance<T>();
        }

        protected override Type[] GetKnownTypes()
        {
            return new Type[] { typeof(T) };
        }

        protected override void SetDirtyObject(object o)
        {
            ((IStateManagedItem)o).SetDirty();
        }

        #region IList<T> Members

        public int IndexOf(T item)
        {
            return ((IList)this).IndexOf(item);
        }

        public void Insert(int index,T item)
        {
            ((IList)this).Insert(index,item);
            if (((IStateManager)this).IsTrackingViewState)
            {
                this.SetDirty();
            }
        }

        public void RemoveAt(int index)
        {
            ((IList)this).RemoveAt(index);
            if (((IStateManager)this).IsTrackingViewState)
            {
                this.SetDirty();
            }
        }

        public T this[int index]
        {
            get { return (T)this[index]; }
            set { this[index] = value; }
        }

        #endregion

        #region ICollection<T> Members

        public void Add(T item)
        {
            ((IList)this).Add(item);
            this.SetDirty();
        }

        public bool Contains(T item)
        {
            return ((IList)this).Contains(item);
        }

        public void CopyTo(T[] array,int arrayIndex)
        {
            ((IList)this).CopyTo(array,arrayIndex);
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(T item)
        {
            if (((IList)this).Contains(item))
            {
                ((IList)this).Remove(item);
                return true;
            }
            return false;
        }

        #endregion


        #region IEnumerable<T> Members

        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable Members

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

        }

        #endregion
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读