2010-05-12 53 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>的其他實現(如果你願意的話)。

相關問題