实现IDictionary接口
来源:互联网 发布:java中级项目源码下载 编辑:程序博客网 时间:2024/05/15 18:07
下面是实现了IDictionary接口的类
public class SerializeDictionary : IDictionary { #region 类成员、方法、构造器 private DictionaryEntry[] Items; private Int32 ItemCount=0; public SerializeDictionary(Int32 itemCount) { this.Items = new DictionaryEntry[itemCount]; } private Boolean TryGetIndexOfKey(Object key, out Int32 index) { for (index = 0; index < ItemCount; index++) { if (this.Items[index].Key.Equals(key)) { return true; } } return false; } #endregion #region IDictionary 成员 public void Add(object key, object value) { if(Items.Length>ItemCount) this.Items[ItemCount + 1] = new DictionaryEntry(key, value); } public void Clear() { this.ItemCount = 0; this.Items = null; } public bool Contains(object key) { Int32 index; return this.TryGetIndexOfKey(key,out index); } public IDictionaryEnumerator GetEnumerator() { return new DictionaryEnumertor(this); } public bool IsFixedSize { get { return false; } } public bool IsReadOnly { get { return false; } } public ICollection Keys { get { Object[] Keys = new Object[ItemCount]; for (Int32 i = 0; i < ItemCount; i++) Keys[i] = Items[i].Key; return Keys; } } public void Remove(object key) { Int32 index; if (TryGetIndexOfKey(key, out index)) { Array.Copy(this.Items,index+1,Items,index,ItemCount-index-1); ItemCount--; } } public ICollection Values { get { Object[] Values = new Object[ItemCount]; for (int i = 0; i < ItemCount; i++) Values[i] = Items[i].Value; return Values; } } public object this[object key] { get { Int32 index; if(TryGetIndexOfKey(key,out index)) return Items[index].Value; return null; } set { Int32 index; if(TryGetIndexOfKey(key, out index)) this.Items[index].Value = value; this.Add(key,value); } } #endregion #region ICollection 成员 public void CopyTo(Array array, int index) { if (index > array.Length||array==null) throw new NotImplementedException("index Or array has error!"); Array.Copy(this.Items, index + 1, array, index, ItemCount - index - 1); } public int Count { get { return this.ItemCount; } } public bool IsSynchronized { get { return true; } } public object SyncRoot { get { lock (this) { if (this.Items == null) throw new NotImplementedException("Is Null"); return new SerializeDictionary(100); } } } #endregion #region IEnumerable 成员 /// <summary> /// dsaaaaaaaaaaaaaaaaaaaa /// </summary> /// <returns></returns> IEnumerator IEnumerable.GetEnumerator() { return this.Items.GetEnumerator(); } #endregion #region 用于迭代的嵌套类 private class DictionaryEnumertor : IDictionaryEnumerator { DictionaryEntry[] Items; Int32 index = -1; public DictionaryEnumertor(SerializeDictionary sd) { Items = new DictionaryEntry[sd.Count]; Array.Copy(sd.Items, 0, Items, 0, sd.Count); } #region IDictionaryEnumerator 成员 public DictionaryEntry Entry { get { return (DictionaryEntry)Items[index]; } } public object Key { get { return Items[index].Key; } } public object Value { get { return Items[index].Value; } } #endregion #region IEnumerator 成员 public object Current { get { return Items[index]; } } public bool MoveNext() { if (index < Items.Length - 1) { index++; return true; } return false; } public void Reset() { index = -1; } #endregion } #endregion }
- 实现IDictionary接口
- IDictionary 泛型接口
- c# IDictionary接口的应用
- IDictionary 序列化一定要实现父类实现了ISerializable接口的,子类也必须有序列化构造函数,否则反序列化时会出错。
- 阐述集合类的接口(IEnumerable,IEnumerator,ICollection,IList,IDictionary)
- 不支持类型 Quartz.JobDataMap,因为它实现 IDictionary。
- 序列化IDictionary 类型
- IDictionary与IDictionaryEnumerator
- wf中的IDictionary
- IDictionary的操作
- C# 关键字集合,IDictionary
- IDictionary遍历删除
- [C++]IDictionary的用法
- 实现接口
- 实现接口
- 接口实现
- 接口实现
- 接口----实现
- 你电脑的右下角有黄色感叹号么?
- 个人日志
- 我们的开源启程纪念
- 首届中南大学杰出校友简介
- 异步网络模型
- 实现IDictionary接口
- 5.3留2
- c#委托知识总结
- 5.3留4
- C++到底还能做什么?
- Oracle插入数据时获取自增ID
- PHP基础学习笔记之基本语法
- 如何让outlook在同步邮件后不删除邮箱里的文件
- nios2