From 727c0dc1fae6c7b72f59fb29f47a52f133618461 Mon Sep 17 00:00:00 2001 From: Jean-Rémy Bancel Date: Wed, 12 Jul 2017 00:25:30 -0700 Subject: C#: Implement IReadOnlyDictionary in MapField --- .../src/Google.Protobuf.Test/Collections/MapFieldTest.cs | 16 ++++++++++++++++ csharp/src/Google.Protobuf/Collections/MapField.cs | 11 +++++++++++ 2 files changed, 27 insertions(+) (limited to 'csharp/src') diff --git a/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs b/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs index 9d3d69af..68b4de45 100644 --- a/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs +++ b/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs @@ -540,6 +540,22 @@ namespace Google.Protobuf.Collections Assert.Throws(() => map.ToString()); } +#if !NET35 + [Test] + public void IDictionaryKeys_Equals_IReadOnlyDictionaryKeys() + { + var map = new MapField { { "foo", "bar" }, { "x", "y" } }; + CollectionAssert.AreEquivalent(((IDictionary)map).Keys, ((IReadOnlyDictionary)map).Keys); + } + + [Test] + public void IDictionaryValues_Equals_IReadOnlyDictionaryValues() + { + var map = new MapField { { "foo", "bar" }, { "x", "y" } }; + CollectionAssert.AreEquivalent(((IDictionary)map).Values, ((IReadOnlyDictionary)map).Values); + } +#endif + private static KeyValuePair NewKeyValuePair(TKey key, TValue value) { return new KeyValuePair(key, value); diff --git a/csharp/src/Google.Protobuf/Collections/MapField.cs b/csharp/src/Google.Protobuf/Collections/MapField.cs index ef5651c9..8dac8e30 100644 --- a/csharp/src/Google.Protobuf/Collections/MapField.cs +++ b/csharp/src/Google.Protobuf/Collections/MapField.cs @@ -67,6 +67,9 @@ namespace Google.Protobuf.Collections /// /// public sealed class MapField : IDeepCloneable>, IDictionary, IEquatable>, IDictionary +#if !NET35 + , IReadOnlyDictionary +#endif { // TODO: Don't create the map/list until we have an entry. (Assume many maps will be empty.) private readonly Dictionary>> map = @@ -548,6 +551,14 @@ namespace Google.Protobuf.Collections } #endregion + #region IReadOnlyDictionary explicit interface implementation +#if !NET35 + IEnumerable IReadOnlyDictionary.Keys => Keys; + + IEnumerable IReadOnlyDictionary.Values => Values; +#endif + #endregion + private class DictionaryEnumerator : IDictionaryEnumerator { private readonly IEnumerator> enumerator; -- cgit v1.2.3