Tópico seguro SortedDictionary
Eu fiz uma classe que usa umSortedDictionary
para armazenar e manipular dados. A classe funciona muito bem, exceto quando implementada em um ambiente multithread. Agora, eu gostaria de tornar o thread de classe seguro escrevendo uma classe de wrapper para o internoSortedDictionary
classe. Gostaria de usar os bloqueios de leitor e gravador para implementar isso, mas, por enquanto, estou tendo problemas para escrever a própria classe de wrapper. Especificamente, não tenho certeza de como implementar oEnumerator
para o dicionário. Aqui está o meu código completo para a classe como está agora.
public class ConcurrentSortedDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
{
#region Variables
SortedDictionary<TKey, TValue> _dict;
#endregion
#region Constructors
public ConcurrentSortedDictionary()
{
_dict = new SortedDictionary<TKey, TValue>();
}
public ConcurrentSortedDictionary(IComparer<TKey> comparer)
{
_dict = new SortedDictionary<TKey, TValue>(comparer);
}
public ConcurrentSortedDictionary(IDictionary<TKey, TValue> dictionary)
{
_dict = new SortedDictionary<TKey, TValue>(dictionary);
}
public ConcurrentSortedDictionary(IDictionary<TKey, TValue> dictionary, IComparer<TKey> comparer)
{
_dict = new SortedDictionary<TKey, TValue>(dictionary, comparer);
}
#endregion
#region Properties
public IComparer<TKey> Comparer
{
get
{
return _dict.Comparer;
}
}
public int Count
{
get
{
return _dict.Count;
}
}
public TValue this[TKey key]
{
get
{
return _dict[key];
}
set
{
_dict[key] = value;
}
}
public SortedDictionary<TKey, TValue>.KeyCollection Keys
{
get
{
return new SortedDictionary<TKey,TValue>.KeyCollection(_dict);
}
}
public SortedDictionary<TKey, TValue>.ValueCollection Values
{
get
{
return new SortedDictionary<TKey, TValue>.ValueCollection(_dict);
}
}
#endregion
#region Methods
public void Add(TKey key, TValue value)
{
_dict.Add(key, value);
}
public void Clear()
{
_dict.Clear();
}
public bool ContainsKey(TKey key)
{
return _dict.ContainsKey(key);
}
public bool ContainsValue(TValue value)
{
return _dict.ContainsValue(value);
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
{
_dict.CopyTo(array, index);
}
public override bool Equals(Object obj)
{
return _dict.Equals(obj);
}
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _dict.GetEnumerator();
}
public override int GetHashCode()
{
return _dict.GetHashCode();
}
public bool Remove(TKey key)
{
return _dict.Remove(key);
}
public override string ToString()
{
return _dict.ToString();
}
public bool TryGetValue(TKey key, out TValue value)
{
return _dict.TryGetValue(key, out value);
}
#endregion
}
Ao compilar o código, recebo a mensagem de erro:
'ConcurrentSortedDictionary' não implementa o membro da interface 'System.Collections.IEnumerable.GetEnumerator ()'. 'ConcurrentSortedDictionary.GetEnumerator ()' não pode implementar 'System.Collections.IEnumerable.GetEnumerator ()' porque não possui o tipo de retorno correspondente de 'System.Collections.IEnumerator'.
Eu olhei para vários posts aqui relacionados a isso como uma referência:
Como implemento IEnumerable na minha classe de wrapper Dictionary que implementa IEnumerable <Foo>? Qual é a melhor maneira de implementar um dicionário seguro para threads?
Mas não vejo o que estou fazendo de errado. Qualquer assistência muito apreciada.