是否有一个通用的具体实施KeyedCollection的? [英] Is there a general concrete implementation of a KeyedCollection?

查看:138
本文介绍了是否有一个通用的具体实施KeyedCollection的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的<一个href="http://msdn.microsoft.com/en-us/library/ms132439.aspx">System.Collections.ObjectModel.KeyedCollection类是一个非常有用的替代<一href="http://msdn.microsoft.com/en-us/library/xfhwa508.aspx">System.Collections.Generic.Dictionary,特别是当关键数据是对象的一部分被存储或要能够枚举在订单中的项目。不幸的是,类是抽象的,我无法找到核心.NET框架一般的具体实现。

href="http://rads.stackoverflow.com/amzn/click/0321545613">框架设计Guidlines 书表明一个具体实施的的提供的从项目到它的密钥转换:

 公共类ConcreteKeyedCollection&LT; TKEY的,TItem&GT; :KeyedCollection&LT; TKEY的,TItem&GT;
{
    私人器&LT; TItem,TKEY的&GT; getKeyForItem = NULL;
    公共ConcreteKeyedCollection(转换器和LT; TItem,TKEY的&GT; getKeyForItem)
    {
        如果(getKeyForItem == NULL){抛出新ArgumentNullException(getKeyForItem); }
        this.getKeyForItem = getKeyForItem;
    }
    保护覆盖TKEY的GetKeyForItem(TItem项)
    {
        返回this.getKeyForItem(项目);
    }
}
 

解决方案

有具体的实现,包括(但不限于):

  • KeyedByTypeCollection&LT; TItem&GT; (我经常使用这个类)
  • <一个href="http://msdn.microsoft.com/en-us/library/system.servicemodel.description.messageheaderdescriptioncollection.aspx"相对=nofollow> MessageHeaderDescriptionCollection

要你的问题的精神,没有就没有通用的实施和因为我对微软不工作,我只能猜测。由于具体的实施是因为你已经证明是容易的,我不会提供任何猜测(因为它很可能是错的)。

The System.Collections.ObjectModel.KeyedCollection class is a very useful alternative to System.Collections.Generic.Dictionary, especially when the key data is part of the object being stored or you want to be able to enumerate the items in order. Unfortunately, the class is abstract, and I am unable to find a general concrete implementation in the core .NET framework.

The Framework Design Guidlines book indicates that a concrete implementation should be provided for abstract types (section 4.4 Abstract Class Design). Why would the framework designers leave out a general concrete implementation of such a useful class, especially when it could be provided by simply exposing a constructor that accepts and stores a Converter from the item to its key:

public class ConcreteKeyedCollection<TKey, TItem> : KeyedCollection<TKey, TItem>
{
    private Converter<TItem, TKey> getKeyForItem = null;
    public ConcreteKeyedCollection(Converter<TItem, TKey> getKeyForItem)
    {
        if (getKeyForItem == null) { throw new ArgumentNullException("getKeyForItem"); }
        this.getKeyForItem = getKeyForItem;
    }
    protected override TKey GetKeyForItem(TItem item)
    {
        return this.getKeyForItem(item);
    }
}

解决方案

There are concrete implementations, including (but not limited to):

To the spirit of your question, no there is no generic implementation and since I do not work for Microsoft I could only speculate. Since the concrete implementation is as easy as you've shown, I won't offer any speculation (as it would probably be wrong).

这篇关于是否有一个通用的具体实施KeyedCollection的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆