KeyedCollection<TKey, TItem>与IDictionary<TKey, TValue>的区别
2009-08-30 21:59
399 查看
Everybody都知道IDictionary<TKey,TValue>表示键/值对的泛型集合,是键/值对的泛型集合的基接口,典型的应用就是字典类Dictionary,但清楚KeyedCollection<TKey,TItem>的应用吗,与IDictionary又有什么区别呢?
它提供集合键嵌入在值中的集合的抽象基类,是基于IList<T>泛型接口的集合与基于IDictionary<T>泛型接口的集合之间的组合体,与基于IList<T>泛型接口的集合一样,KeyedCollection<TKey,TItem>也是项的索引列表,与基于IDictionary<TKey,TValue>泛型接口的集合一样,KeyedCollection<TKey,TItem>包含与每个元素关联的一个键。
与字典不同,KeyedCollection<TKey,TItem>的元素不是键值对;相反整个元素是值,而键嵌入在值内,KeyedCollection<TKey,TItem>中的每个键必须是唯一的,键不能为null引用。
应用实例:(以开源组件json.net的代码举例)
比如有这样一个类:
[align=left]//根据类型获取类的成员集合[/align]
(注释:memberMappings集合中的元素的值是MemberMapping实例)MemberMappong实例键值对形式,键:成员名称;值:成员对象)
它提供集合键嵌入在值中的集合的抽象基类,是基于IList<T>泛型接口的集合与基于IDictionary<T>泛型接口的集合之间的组合体,与基于IList<T>泛型接口的集合一样,KeyedCollection<TKey,TItem>也是项的索引列表,与基于IDictionary<TKey,TValue>泛型接口的集合一样,KeyedCollection<TKey,TItem>包含与每个元素关联的一个键。
与字典不同,KeyedCollection<TKey,TItem>的元素不是键值对;相反整个元素是值,而键嵌入在值内,KeyedCollection<TKey,TItem>中的每个键必须是唯一的,键不能为null引用。
应用实例:(以开源组件json.net的代码举例)
比如有这样一个类:
Code
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Collections.ObjectModel;
namespaceJSON
{
publicclassMemberMappingCollection:KeyedCollection<string,MemberMapping>
{
protectedoverridestringGetKeyForItem(MemberMappingitem)
{
returnitem.MappingName;
}
publicvoidAddMapping(MemberMappingmemberMapping)
{
if(Contains(memberMapping.MappingName))
{
//don'toverwriteexistingmappingwithignoredmapping
if(memberMapping.Ignored)
return;
MemberMappingexistingMemberMapping=this[memberMapping.MappingName];
if(!existingMemberMapping.Ignored)
{
thrownewJsonSerializationException(
string.Format(
"Amemberwiththename'{0}'alreadyexistson{1}.UsetheJsonPropertyAttributetospecifyanothername.",
memberMapping.MappingName,memberMapping.Member.DeclaringType));
}
else
{
//removeignoredmappingsoitcanbereplacedincollection
Remove(existingMemberMapping);
}
}
Add(memberMapping);
}
}
}
这样一个元素值以键值对形式存在的集合就诞生了,下面就举例如何使用这样的一个集合类:
比如有一个Person类:
[code]publicclassPerson
{
publicstringName
{
get;
set;
}
publicintAge
{
get;
set;
}
publicintSalary
{
get;
set;
}
}
[align=left]//根据类型获取类的成员集合[/align]
privateMemberMappingCollectionCreateMemberMappings(TypeobjectType)
{
MemberInfo[]members=objectType.GetMembers();
foreach(MemberInfomemberinmembers)
{
stringmappedName;
MemberMappingmemberMapping=newMemberMapping(member.Name,member,ignored,readable,writable);
memberMappings.AddMapping(memberMapping);
}
returnmemberMappings;
}
//用处
//这样随便传入一个类型便可获得这个类型的成员以键值对形式存在的集合
MemberMappingCollectionmemberMappings=CreateMemberMappings(typeof(Person));
(注释:memberMappings集合中的元素的值是MemberMapping实例)MemberMappong实例键值对形式,键:成员名称;值:成员对象)
相关文章推荐
- c#学习笔记对IEnumerable<T>,IDictionary<Tkey,TValue>,ICollection<T>,IList<T>的总结
- IDictionary<TKey, TValue> 接口(数据词典)讲解与示例应用
- 对IEnumerable<T>,IDictionary<Tkey,TValue>,ICollection<T>,IList<T>的总结
- 在hibernate中 以Collection<Entity> 替换Map<Key, List<Value>结构
- 控制台app.config 的 <add key="aspnet:MaxHttpCollectionKeys" value="10000" />无效
- Failed to set (keyPath) user defined inspected property on (UIView): [<UIView 0x7994f790> setValue:f
- Dictionary、List<KeyValuePair<i>>的组合使用
- c#扩展方法奇思妙用基础篇五:Dictionary<TKey, TValue> 扩展
- KeyValuePair<TKey, TValue>是DictionaryEntry的泛型版本
- iOS- 报错(崩)[< > setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key
- JAXB: how to marshall map into <key>value</key>
- C# - IOrderedDictionary<TKey, TValue> Implementation
- C# KeyValuePair<TKey,TValue>的用法【转】
- wpf list<T>与ObservableCollection<T>的区别
- 提交表单<input type="button" value="登录" onclick="login()"/> 和<button onclick="login()">登录</button>区别
- C# - OrderedDictionary<TKey, TValue> Implementation
- c# 扩展方法奇思妙用基础篇五:Dictionary<TKey, TValue> 扩展
- SortedDictionary<TKey, TValue> 类 表示根据键进行排序的键/值对的集合。
- 取list<Integer,String> list的key和value
- JavaSE8基础 HashMap<Integer,String> containsKey/Value 判断集合是否包含指定的键或值