Fix sizeof struct within generic type instance

#1219
This commit is contained in:
Wojtek Figat
2023-07-03 11:42:15 +02:00
parent aaf349170c
commit 9a1fd12a85
5 changed files with 31 additions and 9 deletions

View File

@@ -46,6 +46,7 @@ namespace FlaxEngine.Interop
#endif
private static Dictionary<object, ManagedHandle> classAttributesCacheCollectible = new();
private static Dictionary<Assembly, ManagedHandle> assemblyHandles = new();
private static Dictionary<Type, int> _typeSizeCache = new();
private static Dictionary<string, IntPtr> loadedNativeLibraries = new();
internal static Dictionary<string, string> nativeLibraryPaths = new();
@@ -584,7 +585,7 @@ namespace FlaxEngine.Interop
else if (fieldType.IsClass || fieldType.IsPointer)
fieldAlignment = IntPtr.Size;
else
fieldAlignment = Marshal.SizeOf(fieldType);
fieldAlignment = GetTypeSize(fieldType);
}
internal static void ToManagedField(FieldInfo field, ref T fieldOwner, IntPtr fieldPtr, out int fieldOffset)
@@ -1088,6 +1089,26 @@ namespace FlaxEngine.Interop
return handle;
}
internal static int GetTypeSize(Type type)
{
if (!_typeSizeCache.TryGetValue(type, out var size))
{
try
{
size = Marshal.SizeOf(type);
}
catch
{
// Workaround the issue where structure defined within generic type instance (eg. MyType<int>.MyStruct) fails to get size
// https://github.com/dotnet/runtime/issues/46426
var obj = Activator.CreateInstance(type);
size = Marshal.SizeOf(obj);
}
_typeSizeCache.Add(type, size);
}
return size;
}
private static class DelegateHelpers
{
#if USE_AOT