mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
ScriptCore Fixed CreateInstanceSafe(Type type, params object[] args)
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
using System;
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace GlitchyEngine.Extensions;
|
||||
|
||||
@@ -7,7 +10,7 @@ public static class ActivatorExtension
|
||||
/// <summary>Creates an instance of the specified type using that type's default constructor.</summary>
|
||||
/// <param name="type">The type of object to create.</param>
|
||||
/// <returns>A reference to the newly created object; or <see langword="null"/> if no instance could be created.</returns>
|
||||
public static object CreateInstanceSafe(Type type)
|
||||
public static object? CreateInstanceSafe(Type type)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -28,4 +31,22 @@ public static class ActivatorExtension
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>Creates an instance of the specified type using a constructor that matches the given arguments.</summary>
|
||||
/// <param name="type">The type of object to create.</param>
|
||||
/// <param name="args">The arguments passed to the constructor</param>
|
||||
/// <returns>A reference to the newly created object; or <see langword="null"/> if no instance could be created.</returns>
|
||||
public static object? CreateInstanceSafe(Type type, params object[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Activator.CreateInstance(type, BindingFlags.Default | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.CreateInstance | BindingFlags.Instance, null, args, null);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error($"Failed to create instance of type \"{type}\": {e}");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user