怎样由属性定位到对象?

bonnibell 2006-09-28 10:16:49
刚接触 ICollection 还有好多不懂的地方

请教: 我得到一个类的 ICollection 也就是得到一个对象的集合

这个类里面有个属性是唯一的标志 比如Name

各个对象的Name 属性是不同的

怎样给定一个Name的值 然后在 ICollection中定位出该对象呢?

期待各位高人的解答.

...全文
123 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bonnibell 2006-09-28
  • 打赏
  • 举报
回复
有点思路了,在去看看
Knight94 2006-09-28
  • 打赏
  • 举报
回复
to 我还是不太明白怎么定位到具体的对象啊?

简单说,其实就是把Name作为Key来存取对象。
bonnibell 2006-09-28
  • 打赏
  • 举报
回复
我还是不太明白怎么定位到具体的对象啊?
lowtemper 2006-09-28
  • 打赏
  • 举报
回复
public new Item this[int index]
{
get
{
return (Item)InnerList[index];
}
set
{
if (index > -1 && index < Count)
{
if (value is Item)
{
//原来的Collection设置为0
((Item)InnerList[index]).Collection = null;
InnerList[index] = value;
//Collection设置为this
((Item)InnerList[index]).Collection = this;
}
}
}
}
Eddie005 2006-09-28
  • 打赏
  • 举报
回复

IDictionary 接口

命名空间:System.Collections
程序集:mscorlib(在 mscorlib.dll 中)

语法

C#
[ComVisibleAttribute(true)]
public interface IDictionary : ICollection, IEnumerable

备注
IDictionary 接口是键/值对的非通用集合的基接口。有关该接口的泛型版本的信息,请参见 System.Collections.Generic.IDictionary。

每个元素都是一个存储在 DictionaryEntry 对象中的键/值对。

每一对都必须有唯一的键。实现在是否允许键为空引用(在 Visual Basic 中为 Nothing) 方面有所不同。此值可以为空引用(在 Visual Basic 中为 Nothing),并且不必是唯一的。IDictionary 接口允许对所包含的键和值进行枚举,但这并不意味着任何特定的排序顺序。

IDictionary 实现有三种类别:只读、固定大小、可变大小。无法修改只读 IDictionary 对象。固定大小的 IDictionary 对象不允许添加或移除元素,但允许修改现有元素。可变大小的 IDictionary 对象允许添加、移除和修改元素。

C# 语言中的 foreach 语句(在 Visual Basic 中为 for each)需要集合中每个元素的类型。由于 IDictionary 对象的每个元素都是一个键/值对,因此元素类型既不是键的类型,也不是值的类型。而是 DictionaryEntry 类型。例如:

C# 复制代码
foreach (DictionaryEntry myDE in myHashtable) {...}

Visual Basic 复制代码
Dim myDE As DictionaryEntry
For Each myDE In myHashtable
...
Next myDE


foreach 语句是对枚举数的包装,它只允许从集合中读取,不允许写入集合。

给实现者的说明 实现类必须有一种方式来对键进行比较。

示例
下面的代码示例演示如何定义实现 IDictionary 接口的简单字典类。

C# 复制代码
// This class implements a simple dictionary using an array of DictionaryEntry objects (key/value pairs).
public class SimpleDictionary : IDictionary
{
// The array of items
private DictionaryEntry[] items;
private Int32 ItemsInUse = 0;

// Construct the SimpleDictionary with the desired number of items.
// The number of items cannot change for the life time of this SimpleDictionary.
public SimpleDictionary(Int32 numItems)
{
items = new DictionaryEntry[numItems];
}


#region IDictionary Members
public bool IsReadOnly { get { return false; } }
public bool Contains(object key)
{
Int32 index;
return TryGetIndexOfKey(key, out index);
}
public bool IsFixedSize { get { return false; } }
public void Remove(object key)
{
if (key == null) throw new ArgumentNullException("key");
// Try to find the key in the DictionaryEntry array
Int32 index;
if (TryGetIndexOfKey(key, out index))
{
// If the key is found, slide all the items up.
Array.Copy(items, index + 1, items, index, ItemsInUse - index - 1);
ItemsInUse--;
}
else
{
// If the key is not in the dictionary, just return.
}
}
public void Clear() { ItemsInUse = 0; }
public void Add(object key, object value)
{
// Add the new key/value pair even if this key already exists in the dictionary.
if (ItemsInUse == items.Length)
throw new InvalidOperationException("The dictionary cannot hold any more items.");
items[ItemsInUse++] = new DictionaryEntry(key, value);
}
public ICollection Keys
{
get
{
// Return an array where each item is a key.
Object[] keys = new Object[ItemsInUse];
for (Int32 n = 0; n < ItemsInUse; n++)
keys[n] = items[n].Key;
return keys;
}
}
public ICollection Values
{
get
{
// Return an array where each item is a value.
Object[] values = new Object[ItemsInUse];
for (Int32 n = 0; n < ItemsInUse; n++)
values[n] = items[n].Value;
return values;
}
}
public object this[object key]
{
get
{
// If this key is in the dictionary, return its value.
Int32 index;
if (TryGetIndexOfKey(key, out index))
{
// The key was found; return its value.
return items[index].Value;
}
else
{
// The key was not found; return null.
return null;
}
}

set
{
// If this key is in the dictionary, change its value.
Int32 index;
if (TryGetIndexOfKey(key, out index))
{
// The key was found; change its value.
items[index].Value = value;
}
else
{
// This key is not in the dictionary; add this key/value pair.
Add(key, value);
}
}
}
private Boolean TryGetIndexOfKey(Object key, out Int32 index)
{
for (index = 0; index < ItemsInUse; index++)
{
// If the key is found, return true (the index is also returned).
if (items[index].Key.Equals(key)) return true;
}

// Key not found, return false (index should be ignored by the caller).
return false;
}
private class SimpleDictionaryEnumerator : IDictionaryEnumerator
{
// A copy of the SimpleDictionary object's key/value pairs.
DictionaryEntry[] items;
Int32 index = -1;

public SimpleDictionaryEnumerator(SimpleDictionary sd)
{
// Make a copy of the dictionary entries currently in the SimpleDictionary object.
items = new DictionaryEntry[sd.Count];
Array.Copy(sd.items, 0, items, 0, sd.Count);
}

// Return the current item.
public Object Current { get { ValidateIndex(); return items[index]; } }

// Return the current dictionary entry.
public DictionaryEntry Entry
{
get { return (DictionaryEntry) Current; }
}

// Return the key of the current item.
public Object Key { get { ValidateIndex(); return items[index].Key; } }

// Return the value of the current item.
public Object Value { get { ValidateIndex(); return items[index].Value; } }

// Advance to the next item.
public Boolean MoveNext()
{
if (index < items.Length - 1) { index++; return true; }
return false;
}

// Validate the enumeration index and throw an exception if the index is out of range.
private void ValidateIndex()
{
if (index < 0 || index >= items.Length)
throw new InvalidOperationException("Enumerator is before or after the collection.");
}

// Reset the index to restart the enumeration.
public void Reset()
{
index = -1;
}
}
public IDictionaryEnumerator GetEnumerator()
{
// Construct and return an enumerator.
return new SimpleDictionaryEnumerator(this);
}
#endregion

#region ICollection Members
public bool IsSynchronized { get { return false; } }
public object SyncRoot { get { throw new NotImplementedException(); } }
public int Count { get { return ItemsInUse; } }
public void CopyTo(Array array, int index) { throw new NotImplementedException(); }
#endregion

#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
// Construct and return an enumerator.
return ((IDictionary)this).GetEnumerator();
}
#endregion
}
bonnibell 2006-09-28
  • 打赏
  • 举报
回复
楼上老大有相关的教程或例子么?
Eddie005 2006-09-28
  • 打赏
  • 举报
回复
实现IDictionary接口

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧