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 } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 如何在mvc4的cshtml视图页面中的html.textb
- ASP.NET ExpressionBuilder语法 – 在img标记内输出AppSett
- 如何找到ASP.NET中的哪个对象无法序列化?
- asp.net-mvc – DDay iCal – 添加一个与会者
- asp.net – 在同一个项目的MVC和Web API中使用InstancePerH
- asp.net Ajax之无刷新评论介绍
- 如何用C#和ADO.NET建立一个数据绑定网格(转载)
- asp.net-mvc – 如何使用ASP.NET MVC 3编辑IEnumerable?
- ASP.NET – 使用WCF Web服务绑定w / AD组的IIS7部署错误500
- asp.net – 重定向到一个页面,其endResponse为true VS Comp
推荐文章
站长推荐
- asp.net – 使用ItemDataBound我只获得每隔一行.
- asp.net-mvc-3 – mvc3 OutputCache RemoveOutpu
- asp.net-mvc-3 – ASP.NET MVC 3 – 在jquery对话
- ASP.NET页面之间传值的方式之Application实例详解
- asp.net-core – 在ASP.NET vNext过滤器中获取注
- asp.net-mvc-3 – HTML.CheckBox MVC3的相同名称
- asp.net-mvc – “无法同时分析32位和64位应用程
- asp.net-mvc – 在IIS 6上托管ASP.NET MVC时是否
- asp.net – 无法加载文件或程序集“System.Web.M
- asp.net-mvc – 在ASP.net MVC 2.0中使用Url.Con
热点阅读