2010-05-12 69 views
0

我正在自己写一个类库来管理Active Directory。什么是最好的.NET 2.0类型来表示.NET 3.5 HashSet <T>?

我有一个接口:

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。什么是最接近的2.0的HashSet(字符串)?

+0

感谢所有您的伟大答案!我不幸只能选择一个。 = P @ Josh的回答最能描述我现在需要什么。但我也会密切关注您的解决方案。衷心感谢! – 2010-05-13 14:34:22

回答

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); 
    } 
} 
2

我会创建自己的HashSet类,并在幕后使用具有空值的字典(仅使用键)。

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>的其他实现(如果你愿意的话)。

相关问题