Fix crash when C# type has missing empty ctor or it throws an exception

#1019
This commit is contained in:
Wojtek Figat
2023-05-10 20:41:58 +02:00
parent ab2cfe89ce
commit 637850d06c

View File

@@ -646,10 +646,19 @@ namespace FlaxEngine.Interop
[UnmanagedCallersOnly]
internal static void ObjectInit(ManagedHandle objectHandle)
{
object obj = objectHandle.Target;
Type type = obj.GetType();
ConstructorInfo ctor = type.GetMember(".ctor", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).First() as ConstructorInfo;
ctor.Invoke(obj, null);
try
{
object obj = objectHandle.Target;
Type type = obj.GetType();
ConstructorInfo ctor = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);
if (ctor == null)
throw new Exception($"Missing empty constructor in type '{type}'.");
ctor.Invoke(obj, null);
}
catch (Exception ex)
{
Debug.LogException(ex);
}
}
[UnmanagedCallersOnly]