From e55ebc16c559e3d04810d8b9b8aa819bfd7f5d80 Mon Sep 17 00:00:00 2001 From: Andy Gerlicher Date: Wed, 15 Feb 2017 21:46:15 -0800 Subject: [PATCH] Fix invalid indexer in HybridDictionary (#1706) Indexer (defined in IDictionary) implemented in a way that recursively calls itself. Nothing called this code, but if it did it would be wrong (stack overflow). Closes #77 --- src/Shared/HybridDictionary.cs | 4 ++-- src/Shared/UnitTests/HybridDictionary_Tests.cs | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Shared/HybridDictionary.cs b/src/Shared/HybridDictionary.cs index 044b8b6bb11..a638a1a3011 100644 --- a/src/Shared/HybridDictionary.cs +++ b/src/Shared/HybridDictionary.cs @@ -349,8 +349,8 @@ public TValue this[TKey key] /// public object this[object key] { - get { return (Object)this[key]; } - set { this[key] = value; } + get { return ((IDictionary)this)[(TKey)key]; } + set { ((IDictionary)this)[(TKey)key] = (TValue)value; } } /// diff --git a/src/Shared/UnitTests/HybridDictionary_Tests.cs b/src/Shared/UnitTests/HybridDictionary_Tests.cs index 5078b614488..50413fb1371 100644 --- a/src/Shared/UnitTests/HybridDictionary_Tests.cs +++ b/src/Shared/UnitTests/HybridDictionary_Tests.cs @@ -193,6 +193,17 @@ public void Medley() } } + [Fact] + private void VerifyHybridDictionaryBaseIndexer() + { + var dict = new HybridDictionary(); + dict[(object) "key"] = "value"; + + Assert.Equal("value", dict["key"]); + Assert.Equal("value", dict[(object)"key"]); + Assert.Equal("key", dict.Keys.First()); + } + /// /// Performs both actions supplied and asserts either both or neither threw ///