diff --git a/src/Heleonix.Reflection/Reflector.cs b/src/Heleonix.Reflection/Reflector.cs index 67dbd53..0255586 100644 --- a/src/Heleonix.Reflection/Reflector.cs +++ b/src/Heleonix.Reflection/Reflector.cs @@ -77,7 +77,15 @@ private static object GetElementAt(object container, object index) { if (container is IDictionary dictionary) { - return dictionary.Contains(index) ? dictionary[index] : null; + foreach (var key in dictionary.Keys) + { + if (key.Equals(index) || Convert.ToString(key) == Convert.ToString(index)) + { + return dictionary[key]; + } + } + + return null; } if (container is IList list) diff --git a/test/Heleonix.Reflection.Tests/Common/Dummies/SubItem.cs b/test/Heleonix.Reflection.Tests/Common/Dummies/SubItem.cs index d4370bf..b239d61 100644 --- a/test/Heleonix.Reflection.Tests/Common/Dummies/SubItem.cs +++ b/test/Heleonix.Reflection.Tests/Common/Dummies/SubItem.cs @@ -37,8 +37,13 @@ public class SubItem public IEnumerable SubSubItemsEnumerableProperty { get; set; } = new Queue(); /// - /// Gets or sets the sub sub items dictionary property. + /// Gets or sets the sub sub items string dictionary property. /// - public Dictionary SubSubItemsDictionaryProperty { get; set; } = new Dictionary(); + public Dictionary SubSubItemsStringDictionaryProperty { get; set; } = new Dictionary(); + + /// + /// Gets or sets the sub sub items int dictionary property. + /// + public Dictionary SubSubItemsIntDictionaryProperty { get; set; } = new Dictionary(); } } \ No newline at end of file diff --git a/test/Heleonix.Reflection.Tests/Reflector.Get.Tests.cs b/test/Heleonix.Reflection.Tests/Reflector.Get.Tests.cs index 0528787..e56fe8c 100644 --- a/test/Heleonix.Reflection.Tests/Reflector.Get.Tests.cs +++ b/test/Heleonix.Reflection.Tests/Reflector.Get.Tests.cs @@ -194,12 +194,12 @@ public static void Get() And("the indexer is in a middle of the memberPath", () => { - And("the collection is a dictionary", () => + And("the collection is a string-keyed dictionary", () => { - instance.SubItemProperty.SubSubItemsDictionaryProperty.Add( + instance.SubItemProperty.SubSubItemsStringDictionaryProperty.Add( "First Key", new SubSubItem { TextProperty = "First Value" }); - memberPath = "SubItemProperty.SubSubItemsDictionaryProperty[First Key].TextProperty"; + memberPath = "SubItemProperty.SubSubItemsStringDictionaryProperty[First Key].TextProperty"; Should("provide the target value", () => { @@ -209,7 +209,32 @@ public static void Get() And("the key does not exist", () => { - memberPath = "SubItemProperty.SubSubItemsDictionaryProperty[NO KEY].TextProperty"; + memberPath = "SubItemProperty.SubSubItemsStringDictionaryProperty[NO KEY].TextProperty"; + + Should("provide the default value and return false", () => + { + Assert.That(result, Is.Null); + Assert.That(returnValue, Is.False); + }); + }); + }); + + And("the collection is an int-keyed dictionary", () => + { + instance.SubItemProperty.SubSubItemsIntDictionaryProperty.Add( + 12345, + new SubSubItem { TextProperty = "First Value" }); + memberPath = "SubItemProperty.SubSubItemsIntDictionaryProperty[12345].TextProperty"; + + Should("provide the target value", () => + { + Assert.That(result, Is.EqualTo("First Value")); + Assert.That(returnValue, Is.True); + }); + + And("the key does not exist", () => + { + memberPath = "SubItemProperty.SubSubItemsStringDictionaryProperty[111].TextProperty"; Should("provide the default value and return false", () => { diff --git a/test/Heleonix.Reflection.Tests/Reflector.Set.Tests.cs b/test/Heleonix.Reflection.Tests/Reflector.Set.Tests.cs index c43a6ce..343aa84 100644 --- a/test/Heleonix.Reflection.Tests/Reflector.Set.Tests.cs +++ b/test/Heleonix.Reflection.Tests/Reflector.Set.Tests.cs @@ -206,25 +206,25 @@ public static void Set() And("the indexer is in a middle of the memberPath", () => { - And("the collection is a dictionary", () => + And("the collection is a string-keyed dictionary", () => { - instance.SubItemProperty.SubSubItemsDictionaryProperty.Add( + instance.SubItemProperty.SubSubItemsStringDictionaryProperty.Add( "First Key", new SubSubItem { TextProperty = "First Value" }); - memberPath = "SubItemProperty.SubSubItemsDictionaryProperty[First Key].TextProperty"; + memberPath = "SubItemProperty.SubSubItemsStringDictionaryProperty[First Key].TextProperty"; value = "New First Value"; Should("set the value and return true", () => { Assert.That( - instance.SubItemProperty.SubSubItemsDictionaryProperty["First Key"].TextProperty, + instance.SubItemProperty.SubSubItemsStringDictionaryProperty["First Key"].TextProperty, Is.EqualTo(value)); Assert.That(returnValue, Is.True); }); And("the key does not exist", () => { - memberPath = "SubItemProperty.SubSubItemsDictionaryProperty[NO KEY].TextProperty"; + memberPath = "SubItemProperty.SubSubItemsStringDictionaryProperty[NO KEY].TextProperty"; Should("return false", () => {