Strong-Type Collection
来源:互联网 发布:与人工智能有关的股票 编辑:程序博客网 时间:2024/05/16 14:07
在泛型类出现之前:
实现ICollection、IEnumerable、IList接口的目的是要创建对象与.NET Framework之间兼容互通的管道,但这样的做法却会引发另一个问题,那就是这些接口都使用object作为Collection中的元素类型,而.NET Framework所有的对象皆是源自object,这代表着用户可以放入所有对象于Collection对象中。往好的方面着眼,这是最具扩展性的设计方式,但往坏的方面着眼时,这种设计过于松散,容易令用户产生混淆与误用。那该如何兼顾两者呢?答案是Strong-Type Collection,下面是MyList集合的Strong-Type版本。
public class MyList: IList{ priavte ArrayList list; public MyList(){ list = new ArrayList(); } #region IList Members public bool IsReadOnly { get{ return false; } } object IList.this[int index] { get{ return list[index]; } set{ list[index] =value; } } public string this[index] { get{ return (string)list[index]; } set{ list[index] =value; } } public void RemoveAt(int index) { list.Remove(index); } public IList.Insert(int index,object value) { list.Insert(index,value); } //strong-type Insert public void Insert(int index,string value) { ((IList)this).Insert(index,value); } void IList.Remove(object value) { list.Remove(value); } //strong-type remove public void Remove(string value) { ((IList)this).Remove(value); } bool IList.Contains(object value) { return list.Contains(value); } //strong-type contains public bool Contains(string value) { return ((IList)this).Contains(value); } int IList.IndexOf(object value) { return list.IndexOf(value); } //strong-value contains public int IndexOf(string value) { return ((IList)this).IndexOf(value); } int IList.Add(object value) { return list.Add(value); } //strong-type Add. public int Add(string value) { return ((IList)this).Add(value); } public void Clear() { list.Clear(); } public bool IsFixedSize { get{ return false; } } #endregion #region ICollection Members public bool IsSynchronized; { get{ return list.IsSynchronized; } } public int Count{ get{ return list.Count; } } public void CopyTo(Array array,int index) { list.CopyTo(array,index); } public object SyncRoot { get { return list.SyncRoot; } } #endregion #region IEnumberable Members public IEnumberator GetEnumberator() { return list.GetEnumerator(); } #endregion }
MyList将实现IList接口的函数与属性声明为IList接口的明确实现,一旦声明成明确实现,用户就无法在不转型成IList接口情况下调用这些函数,这可以有效防止误用的情况发生。只是没有这些函数的话,用户就无法操作此Collection了,因此MyList提供了另一组同名称的函数与属性给用户,并且以string为操作类型,这样就不会发生误用的情况了。是的!这样一来MyList的实现变得复杂了不少,但在鱼与熊掌要兼得得愿望下,付出点代价是难免的。
CollectionBase
针对设计人员对Strong-type Collection的需求,.NET Framework中提供了一个名为CollectionBase的基础类,其实此类的实现如同MyList类一样,提供了IList接口的基本实现,设计人员可以继承此类来加上额外的同名函数。
public class MyList2:CollectionBase{ public void Insert(string s) { List.Add(s); } public void Remove(string s) { List.Remove(s); } public void Add(string s) { List.Add(s); } public bool Contains(string s) { return List.Contains(s); } public int IndexOf(string s) { return List.IndexOf(s); }}
- Strong-Type Collection
- Asp.net Strong type Session
- setParameterList(String ,collection,Type);
- hive collection data type
- Property with 'retain (or strong)' attribute must be of object type
- Property with 'retain(or strong) attribute must be of object type
- 『IOS』Property with 'retain (or strong)' attribute must be of object type
- AFNetworking 2.0 ERROR:Property with 'retain (or strong)' attribute must be of object type [duplicat
- Property with 'retain(or strong)' attribute must be of object type
- [问题记录]Property with 'retain (or strong)' attribute must be of object type
- Property with 'retain (or strong)' attribute must be of object type 解决方法
- Property with 'retain (or strong)' attribute must be of object type
- warning: non-variable type argument Any in type pattern scala.collection.immutable.Set[Any] (the und
- warning: non-variable type argument Int in type pattern scala.collection.mutable.WrappedArray[Int] i
- collection
- collection
- Collection
- collection
- 如何查看哪个程序在使用80端口
- Stop Using Page Activate PeopleCode
- Steps to install Apex 4 on linux
- jquery通过struts2请求,返回json数据,在jsp页面形成二级联动下拉列表
- ibus安装搜狗的词库
- Strong-Type Collection
- 终于有篇博文访问破百了
- conversion to dalvik format failed with error 1
- Android: How to Capture Screen in Gingerbread(2.3中实现截屏)(续)
- Unable to resolve superclass of错误的解决方法
- 运维相关技术博客
- 【NetBeans】Netbeans中配置C/C++编译环境
- 今年全球半导体销售额将增1.3% 首破3000亿美元
- OpenGL绘图函数