下面是实现了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
}