Skip to content

Commit

Permalink
Fix resx test name mangling logic (#39569)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrabYourPitchforks authored Jul 18, 2020
1 parent bb27de1 commit 6af0d0e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions src/libraries/System.Resources.Extensions/tests/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,8 @@ public override void BindToName(Type serializedType, out string assemblyName, ou
// workaround for https://github.com/dotnet/runtime/issues/31289
assemblyQualifiedTypeName = assemblyQualifiedTypeName.Replace(s_coreAssemblyName, s_mscorlibAssemblyName);

int pos = assemblyQualifiedTypeName.IndexOf(',');
if (pos > 0 && pos < assemblyQualifiedTypeName.Length - 1)
if (TryDeconstructFullyQualifiedTypeName(assemblyQualifiedTypeName, out string newTypeName, out assemblyName))
{
assemblyName = assemblyQualifiedTypeName.Substring(pos + 1).TrimStart();
string newTypeName = assemblyQualifiedTypeName.Substring(0, pos);
if (!string.Equals(newTypeName, serializedType.FullName, StringComparison.InvariantCulture))
{
typeName = newTypeName;
Expand All @@ -322,6 +319,48 @@ public override Type BindToType(string assemblyName, string typeName)
// We should never be using this binder during Deserialization
throw new NotSupportedException($"{nameof(TypeNameManglingSerializationBinder)}.{nameof(BindToType)} should not be used during testing.");
}

private static bool TryDeconstructFullyQualifiedTypeName(string assemblyQualifiedTypeName, out string typeName, out string assemblyName)
{
// Skip over all generic arguments in the assembly-qualified type name.

int genericDepth = 0;
int i;
for (i = 0; i < assemblyQualifiedTypeName.Length; i++)
{
switch (assemblyQualifiedTypeName[i])
{
case '[':
checked { genericDepth++; }
break;

case ']':
checked { genericDepth--; }
break;

case ',' when genericDepth == 0:
goto AfterLoop;

default:
continue;
}
}

AfterLoop:

if (i < assemblyQualifiedTypeName.Length - 1)
{
// Found a proper fully-qualified type name with assembly!
typeName = assemblyQualifiedTypeName.Substring(0, i);
assemblyName = assemblyQualifiedTypeName.Substring(i + 1).Trim();
return true;
}

// Couldn't find an assembly after the type name.
typeName = default;
assemblyName = default;
return false;
}
}
}
}
Binary file modified src/libraries/System.Resources.Extensions/tests/TestData.resources
Binary file not shown.

0 comments on commit 6af0d0e

Please sign in to comment.