From 8d4e38a86fc87690507a14fb0704c9ea5fbfeab2 Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Wed, 1 Feb 2017 21:17:15 +0100 Subject: [PATCH] [C] throw meaningful exception on duplicate RD key (#716) --- Xamarin.Forms.Core.UnitTests/ResourceDictionaryTests.cs | 14 ++++++++++++++ Xamarin.Forms.Core/ResourceDictionary.cs | 2 ++ 2 files changed, 16 insertions(+) diff --git a/Xamarin.Forms.Core.UnitTests/ResourceDictionaryTests.cs b/Xamarin.Forms.Core.UnitTests/ResourceDictionaryTests.cs index e5b8b60..9a2c6f0 100644 --- a/Xamarin.Forms.Core.UnitTests/ResourceDictionaryTests.cs +++ b/Xamarin.Forms.Core.UnitTests/ResourceDictionaryTests.cs @@ -292,5 +292,19 @@ namespace Xamarin.Forms.Core.UnitTests Assert.True(rd0.TryGetMergedValue("foo", out _)); Assert.AreEqual("Foo", _); } + + [Test] + public void ThrowOnDuplicateKey() + { + var rd0 = new ResourceDictionary(); + rd0.Add("foo", "Foo"); + try { + rd0.Add("foo", "Bar"); + } catch (ArgumentException ae) { + Assert.AreEqual("A resource with the key 'foo' is already present in the ResourceDictionary.", ae.Message); + Assert.Pass(); + } + Assert.Fail(); + } } } \ No newline at end of file diff --git a/Xamarin.Forms.Core/ResourceDictionary.cs b/Xamarin.Forms.Core/ResourceDictionary.cs index ed3ea7f..a213dee 100644 --- a/Xamarin.Forms.Core/ResourceDictionary.cs +++ b/Xamarin.Forms.Core/ResourceDictionary.cs @@ -72,6 +72,8 @@ namespace Xamarin.Forms public void Add(string key, object value) { + if (ContainsKey(key)) + throw new ArgumentException($"A resource with the key '{key}' is already present in the ResourceDictionary."); _innerDictionary.Add(key, value); OnValueChanged(key, value); } -- 2.7.4