Fixed deserialization of generic types

This commit is contained in:
Simon Lübeß
2024-01-15 01:18:13 +01:00
parent 19d70c94e6
commit 49db3a28c8
3 changed files with 101 additions and 17 deletions
+93
View File
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
@@ -65,4 +67,95 @@ public static class TypeExtension
return attribute != null;
}
private static ReadOnlySpan<char> ReadTypeName(ReadOnlySpan<char> fullName)
{
for (int i = 0; i < fullName.Length; i++)
{
if (!char.IsLetterOrDigit(fullName[i]) && fullName[i] != '.' && fullName[i] != '+' && fullName[i] != '_')
{
return fullName.Slice(0, i);
}
}
return fullName;
}
public static Type? FindType(string fullName)
{
ReadOnlySpan<char> rest = new ReadOnlySpan<char>();
return FindType(fullName.AsSpan(), ref rest);
}
private static Type? FindType(ReadOnlySpan<char> fullName, ref ReadOnlySpan<char> rest)
{
rest = fullName;
ReadOnlySpan<char> typeName = ReadTypeName(fullName);
rest = rest.Slice(typeName.Length);
if (rest.IsEmpty || rest[0] != '`')
{
// Non generic type, easy!
return GetType(typeName);
}
// Handle generic type
int brackedIndex = fullName.IndexOf('[');
typeName = fullName.Slice(0, brackedIndex);
rest = fullName.Slice(brackedIndex + 1).Trim();
Type? genericType = GetType(typeName);
Console.WriteLine($"Generic Type: {genericType}");
List<Type> arguments = new List<Type>();
while (true)
{
Type? argument = FindType(rest, ref rest);
Console.WriteLine($"Argument: {argument}");
Debug.Assert(argument != null);
arguments.Add(argument!);
rest = rest.TrimStart();
if (rest.TrimStart()[0] == ',')
{
rest = rest.Slice(1);
}
else if (rest.TrimStart()[0] == ']')
{
rest = rest.Slice(1);
break;
}
else
{
break;
}
}
return genericType?.MakeGenericType(arguments.ToArray());
}
private static Type? GetType(ReadOnlySpan<char> fullName)
{
string name = fullName.TrimEnd(']').ToString();
// Non generic type, easy!
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies().Reverse())
{
Type? type = assembly.GetType(name);
if (type != null)
return type;
}
return null;
}
}