Commit 727c0dc1 authored by Jean-Rémy Bancel's avatar Jean-Rémy Bancel Committed by Jon Skeet

C#: Implement IReadOnlyDictionary<K,V> in MapField<K,V>

parent 9ab7c73f
...@@ -540,6 +540,22 @@ namespace Google.Protobuf.Collections ...@@ -540,6 +540,22 @@ namespace Google.Protobuf.Collections
Assert.Throws<ArgumentException>(() => map.ToString()); Assert.Throws<ArgumentException>(() => map.ToString());
} }
#if !NET35
[Test]
public void IDictionaryKeys_Equals_IReadOnlyDictionaryKeys()
{
var map = new MapField<string, string> { { "foo", "bar" }, { "x", "y" } };
CollectionAssert.AreEquivalent(((IDictionary<string, string>)map).Keys, ((IReadOnlyDictionary<string, string>)map).Keys);
}
[Test]
public void IDictionaryValues_Equals_IReadOnlyDictionaryValues()
{
var map = new MapField<string, string> { { "foo", "bar" }, { "x", "y" } };
CollectionAssert.AreEquivalent(((IDictionary<string, string>)map).Values, ((IReadOnlyDictionary<string, string>)map).Values);
}
#endif
private static KeyValuePair<TKey, TValue> NewKeyValuePair<TKey, TValue>(TKey key, TValue value) private static KeyValuePair<TKey, TValue> NewKeyValuePair<TKey, TValue>(TKey key, TValue value)
{ {
return new KeyValuePair<TKey, TValue>(key, value); return new KeyValuePair<TKey, TValue>(key, value);
......
...@@ -67,6 +67,9 @@ namespace Google.Protobuf.Collections ...@@ -67,6 +67,9 @@ namespace Google.Protobuf.Collections
/// </para> /// </para>
/// </remarks> /// </remarks>
public sealed class MapField<TKey, TValue> : IDeepCloneable<MapField<TKey, TValue>>, IDictionary<TKey, TValue>, IEquatable<MapField<TKey, TValue>>, IDictionary public sealed class MapField<TKey, TValue> : IDeepCloneable<MapField<TKey, TValue>>, IDictionary<TKey, TValue>, IEquatable<MapField<TKey, TValue>>, IDictionary
#if !NET35
, IReadOnlyDictionary<TKey, TValue>
#endif
{ {
// TODO: Don't create the map/list until we have an entry. (Assume many maps will be empty.) // TODO: Don't create the map/list until we have an entry. (Assume many maps will be empty.)
private readonly Dictionary<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>> map = private readonly Dictionary<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>> map =
...@@ -548,6 +551,14 @@ namespace Google.Protobuf.Collections ...@@ -548,6 +551,14 @@ namespace Google.Protobuf.Collections
} }
#endregion #endregion
#region IReadOnlyDictionary explicit interface implementation
#if !NET35
IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys => Keys;
IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values => Values;
#endif
#endregion
private class DictionaryEnumerator : IDictionaryEnumerator private class DictionaryEnumerator : IDictionaryEnumerator
{ {
private readonly IEnumerator<KeyValuePair<TKey, TValue>> enumerator; private readonly IEnumerator<KeyValuePair<TKey, TValue>> enumerator;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment