From a50326fc77b156d3a8455c8632b9740eb4db17ca Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Wed, 4 Nov 2020 23:49:25 +0100 Subject: [PATCH] Use object type for key types --- .../src/System/Resources/ResourceSet.cs | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Resources/ResourceSet.cs b/src/libraries/System.Private.CoreLib/src/System/Resources/ResourceSet.cs index 5e920be3e6257..6136c51f6e575 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Resources/ResourceSet.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Resources/ResourceSet.cs @@ -20,14 +20,14 @@ public class ResourceSet : IDisposable, IEnumerable { protected IResourceReader Reader = null!; - private Dictionary? _table; + private Dictionary? _table; private Dictionary? _caseInsensitiveTable; // For case-insensitive lookups. protected ResourceSet() { // To not inconvenience people subclassing us, we should allocate a new // hashtable here just so that Table is set to something. - _table = new Dictionary(); + _table = new Dictionary(); } // For RuntimeResourceSet, ignore the Table parameter - it's a wasted @@ -190,10 +190,7 @@ protected virtual void ReadResources() IDictionaryEnumerator en = Reader.GetEnumerator(); while (en.MoveNext()) { - if (en.Key is not string key) - continue; - - _table.Add(key, en.Value); + _table.Add(en.Key, en.Value); } // While technically possible to close the Reader here, don't close it // to help with some WinRes lifetime issues. @@ -204,7 +201,7 @@ protected virtual void ReadResources() if (name == null) throw new ArgumentNullException(nameof(name)); - Dictionary? copyOfTable = _table; // Avoid a race with Dispose + Dictionary? copyOfTable = _table; // Avoid a race with Dispose if (copyOfTable == null) throw new ObjectDisposedException(null, SR.ObjectDisposed_ResourceSet); @@ -215,7 +212,7 @@ protected virtual void ReadResources() private object? GetCaseInsensitiveObjectInternal(string name) { - Dictionary? copyOfTable = _table; // Avoid a race with Dispose + Dictionary? copyOfTable = _table; // Avoid a race with Dispose if (copyOfTable == null) throw new ObjectDisposedException(null, SR.ObjectDisposed_ResourceSet); @@ -223,7 +220,14 @@ protected virtual void ReadResources() Dictionary? caseTable = _caseInsensitiveTable; // Avoid a race condition with Close if (caseTable == null) { - caseTable = new Dictionary(copyOfTable, StringComparer.OrdinalIgnoreCase); + caseTable = new Dictionary(copyOfTable.Count, StringComparer.OrdinalIgnoreCase); + foreach (var item in copyOfTable) + { + if (item.Key is not string s) + continue; + + caseTable.Add(s, item.Value); + } _caseInsensitiveTable = caseTable; }