Skip to content

Commit

Permalink
Use object type for key types
Browse files Browse the repository at this point in the history
  • Loading branch information
marek-safar committed Nov 5, 2020
1 parent 052f8eb commit a50326f
Showing 1 changed file with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public class ResourceSet : IDisposable, IEnumerable
{
protected IResourceReader Reader = null!;

private Dictionary<string, object?>? _table;
private Dictionary<object, object?>? _table;
private Dictionary<string, object?>? _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<string, object?>();
_table = new Dictionary<object, object?>();
}

// For RuntimeResourceSet, ignore the Table parameter - it's a wasted
Expand Down Expand Up @@ -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.
Expand All @@ -204,7 +201,7 @@ protected virtual void ReadResources()
if (name == null)
throw new ArgumentNullException(nameof(name));

Dictionary<string, object?>? copyOfTable = _table; // Avoid a race with Dispose
Dictionary<object, object?>? copyOfTable = _table; // Avoid a race with Dispose

if (copyOfTable == null)
throw new ObjectDisposedException(null, SR.ObjectDisposed_ResourceSet);
Expand All @@ -215,15 +212,22 @@ protected virtual void ReadResources()

private object? GetCaseInsensitiveObjectInternal(string name)
{
Dictionary<string, object?>? copyOfTable = _table; // Avoid a race with Dispose
Dictionary<object, object?>? copyOfTable = _table; // Avoid a race with Dispose

if (copyOfTable == null)
throw new ObjectDisposedException(null, SR.ObjectDisposed_ResourceSet);

Dictionary<string, object?>? caseTable = _caseInsensitiveTable; // Avoid a race condition with Close
if (caseTable == null)
{
caseTable = new Dictionary<string, object?>(copyOfTable, StringComparer.OrdinalIgnoreCase);
caseTable = new Dictionary<string, object?>(copyOfTable.Count, StringComparer.OrdinalIgnoreCase);
foreach (var item in copyOfTable)
{
if (item.Key is not string s)
continue;

caseTable.Add(s, item.Value);
}
_caseInsensitiveTable = caseTable;
}

Expand Down

0 comments on commit a50326f

Please sign in to comment.