表示.NET 3.5 HashSet <t>的最佳.NET 2.0类型是什么?</t>

时间:2010-05-12 18:13:59

标签: c# vb.net generics .net-3.5 .net-2.0

我正在为自己编写一个类库来管理Active Directory。

我有一个界面:

Public Interface ISourceAnnuaire(Of T as {IGroupe, ITop, IUniteOrganisation, IUtilisateur})
    Readonly Property Changements As Dictionary(Of T, HashSet(Of String))
End Interface

此Changements属性用于在内存中保存属于源的特定元素上发生的更改。

但是,我坚持使用.NET Framework 2.0。什么是最接近的HashSet(Of String)的.NET 2.0?

4 个答案:

答案 0 :(得分:3)

使用非通用Hashtable或破解字典并使用它的密钥集。

Public class HashSetHack<T> : //Whatever collection interfaces you need.
{
    private readonly Dictionary<T, object> dict = new Dictionary<T, object>();

    //whatever code you need to wrap the interfaces using dict.Keys eg:

    public void Add(T value)
    {
      dict.add(value, null);
    }
}

答案 1 :(得分:2)

我会创建自己的HashSet类,在幕后使用带空值的Dictionary(仅使用键)。

答案 2 :(得分:1)

这是一种特别灵活的方法:

public abstract class UniqueSet<T, TDictionary> : ICollection<T>
    where TDictionary : IDictionary<T, byte> {

    protected TDictionary _internalDictionary;

    protected UniqueSet(TDictionary dictionary) {
        _internalDictionary = dictionary;
    }

    // implement the ICollection<T> interface
    // using your internal dictionary's Keys property

    // for example:
    public void Add(T value) {
        _internalDictionary.Add(value, 0);
    }

    // etc.

}

public class UniqueSet<T> : UniqueSet<T, Dictionary<T, byte>> {

    public UniqueSet() : base(new Dictionary<T, byte>()) { }

}

为什么抽象基类,你问?好吧,通过这种方法,你也可以实现一个SortedUniqueSet<T> SortedList<T, byte>作为其内部集合(这可以实现IList<T>) - 而不必编写任何更多的代码。您还可以利用您偶然发现的IDictionary<TKey, TValue>的任何其他实现(如果您选择的话)。

答案 3 :(得分:1)

Power Collections库中的Set<T>类与HashSet<T>几乎完全相同。它适用于.NET 2.0。