Fixed TypeExtension.FindType for array types

This commit is contained in:
Simon Lübeß
2026-09-14 22:05:10 +02:00
parent c7fb9af0c7
commit d2c6c9a948
2 changed files with 83 additions and 34 deletions
+51 -3
View File
@@ -95,12 +95,56 @@ public static class TypeExtension
ReadOnlySpan<char> typeName = ReadTypeName(fullName); ReadOnlySpan<char> typeName = ReadTypeName(fullName);
rest = rest.Slice(typeName.Length); rest = rest.Slice(typeName.Length);
if (rest.IsEmpty || rest[0] != '`') Type? baseType = GetType(typeName);
if (baseType == null)
{ {
// Non generic type, easy! return null;
return GetType(typeName);
} }
if (rest.IsEmpty)
{
// Non generic type, easy!
return baseType;
}
if (rest[0] == '[')
{
// Handle array type
int rank = 1;
bool forceMultiDimensional = false;
for (int i = 0; i < rest.Length; i++)
{
if (rest[i] == ',')
{
rank++;
}
else if (rest[i] == '*')
{
forceMultiDimensional = true;
}
else if (rest[i] == ']')
{
rest = rest.Slice(i + 1);
break;
}
}
Debug.Assert(rest.IsEmpty);
// It is important to use MakeArrayType() for single-dimensional arrays, as a single-dimension array is distinct from a multi-dimension array of rank 1. The latter cannot be created using normal code, but can be created using reflection.
if (rank == 1 && !forceMultiDimensional)
{
return baseType.MakeArrayType();
}
else
{
return baseType.MakeArrayType(rank);
}
}
if (rest[0] == '`')
{
// Handle generic type // Handle generic type
int brackedIndex = fullName.IndexOf('['); int brackedIndex = fullName.IndexOf('[');
@@ -143,6 +187,10 @@ public static class TypeExtension
return genericType?.MakeGenericType(arguments.ToArray()); return genericType?.MakeGenericType(arguments.ToArray());
} }
Log.Error($"Failed to find a type for {typeName}.");
return null;
}
private static Type? GetType(ReadOnlySpan<char> fullName) private static Type? GetType(ReadOnlySpan<char> fullName)
{ {
string name = fullName.TrimEnd(']').ToString(); string name = fullName.TrimEnd(']').ToString();
@@ -56,6 +56,7 @@ public class DeserializationObject
{ {
ScriptGlue.Serialization_GetObjectTypeName(_internalContext, out string fullTypeName); ScriptGlue.Serialization_GetObjectTypeName(_internalContext, out string fullTypeName);
// TODO: Retrieving the entire type by name is not ideal, we should probably consider using a more efficient approach.
return GetTypeFromName(fullTypeName); return GetTypeFromName(fullTypeName);
} }
} }