using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace SFire.Framework
{
/// <summary>
/// Silverlight下使用的哈希表
/// 创建者:sucsy
/// 创建日期:2012-2-27
/// </summary>
public class DataHashtable : IDictionary
{
List<HashTableItem> table = null;
public DataHashtable()
{
this.table = new List<HashTableItem>();
}
public void Add(object key, object value)
{
if (this.Contains(key) == false)
{
table.Add
(
new HashTableItem()
{
Key = key,
Value = value
}
);
}
else
{
this[key] = value;
}
}
public void Clear()
{
table.Clear();
}
public bool Contains(object key)
{
foreach (HashTableItem item in this.table)
{
if (item.Key!=null && item.Key.Equals(key)) return true;
}
return false;
}
public IDictionaryEnumerator GetEnumerator()
{
return (IDictionaryEnumerator)this.table.ToArray().GetEnumerator();
}
public bool IsFixedSize
{
get { return false; }
}
public bool IsReadOnly
{
get { return false; }
}
public ICollection Keys
{
get { return this.table.Select(item => item.Key).ToArray(); }
}
public void Remove(object key)
{
HashTableItem item = Find(key);
if (item != null) this.table.Remove(item);
}
private HashTableItem Find(object key)
{
HashTableItem find = null;
foreach (HashTableItem item in this.table)
{
if (item.Key.Equals(key))
{
find = item;
break;
}
}
return find;
}
public ICollection Values
{
get { return this.table.Select(item => item.Value).ToArray(); }
}
public object this[object key]
{
get
{
HashTableItem item = Find(key);
if (item != null) return item.Value;
return null;
}
set
{
HashTableItem item = Find(key);
if (item != null) item.Value = value;
}
}
public void CopyTo(Array array, int index)
{
this.table.CopyTo((HashTableItem[])array, index);
}
public int Count
{
get { return this.table.Count; }
}
public bool IsSynchronized
{
get { return true; }
}
public object SyncRoot
{
get { return null; }
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.table.GetEnumerator();
}
}
#region 哈希表项
/// <summary>
/// 哈希表项
/// </summary>
public class HashTableItem
{
/// <summary>
/// 关键字
/// </summary>
public object Key { get; set; }
/// <summary>
/// 源对象
/// </summary>
public object Value { get; set; }
}
#endregion
}
Silverlight 下创建Hashtable
最新推荐文章于 2020-05-14 15:14:32 发布