Skip to content

Commit

Permalink
Refactor StronglyTypedResourceBuilder to replace Hashtable with `…
Browse files Browse the repository at this point in the history
…Dictionary` (#8677)

* Refactor StronglyTypedResourceBuilder to replace Hashtable with Dictionary

* fix dictionary lookup

* Change from review to use TryGetValue

* changes from review
  • Loading branch information
elachlan authored Feb 23, 2023
1 parent 9879536 commit 3b6aac5
Showing 1 changed file with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ private static CodeCompileUnit InternalCreate(

// Verify the resource names are valid property names, and they don't conflict. This includes checking for
// language-specific keywords, translating spaces to underscores, etc.
var cleanedResourceList = VerifyResourceNames(resourceList, codeProvider, errors, out Hashtable reverseFixupTable);
var cleanedResourceList = VerifyResourceNames(resourceList, codeProvider, errors, out Dictionary<string, string> reverseFixupTable);

// Verify the class name is legal.
string className = baseName;
Expand Down Expand Up @@ -418,7 +418,8 @@ private static CodeCompileUnit InternalCreate(
foreach ((string propertyName, ResourceData resource) in cleanedResourceList)
{
// The resourceName will be the original value, before fixups, if any.
string resourceName = (string)reverseFixupTable[propertyName] ?? propertyName;
reverseFixupTable.TryGetValue(propertyName, out string resourceName);
resourceName ??= propertyName;
if (!DefineResourceFetchingProperty(
propertyName,
resourceName,
Expand Down Expand Up @@ -861,9 +862,9 @@ private static SortedList<string, ResourceData> VerifyResourceNames(
Dictionary<string, ResourceData> resourceList,
CodeDomProvider codeProvider,
List<string> errors,
out Hashtable reverseFixupTable)
out Dictionary<string, string> reverseFixupTable)
{
reverseFixupTable = new Hashtable(0, StringComparer.InvariantCultureIgnoreCase);
reverseFixupTable = new(0, StringComparer.InvariantCultureIgnoreCase);
SortedList<string, ResourceData> cleanedResourceList = new(resourceList.Count, StringComparer.InvariantCultureIgnoreCase);

foreach (KeyValuePair<string, ResourceData> entry in resourceList)
Expand Down Expand Up @@ -898,8 +899,7 @@ private static SortedList<string, ResourceData> VerifyResourceNames(
}

// Now see if we've already mapped another key to the same name.
string oldDuplicateKey = (string)reverseFixupTable[newKey];
if (oldDuplicateKey is not null)
if (reverseFixupTable.TryGetValue(newKey, out string oldDuplicateKey) && oldDuplicateKey is not null)
{
// We can't handle this key nor the previous one. Remove the old one.
if (!errors.Contains(oldDuplicateKey))
Expand Down Expand Up @@ -929,8 +929,7 @@ private static SortedList<string, ResourceData> VerifyResourceNames(
{
// There was a case-insensitive conflict between two keys. Or possibly one key was fixed up in a
// way that conflicts with another key (ie, "A B" and "A_B").
string fixedUp = (string)reverseFixupTable[key];
if (fixedUp is not null)
if (reverseFixupTable.TryGetValue(key, out string fixedUp) && fixedUp is not null)
{
if (!errors.Contains(fixedUp))
{
Expand Down

0 comments on commit 3b6aac5

Please sign in to comment.