Replace exceptions with null checks to avoid event errors.

This commit is contained in:
2025-11-04 06:59:22 +01:00
parent 21b9cdc77c
commit ea125c7c8f
2 changed files with 12 additions and 23 deletions

View File

@@ -31,14 +31,9 @@ namespace SerializableFunc.Runtime
private Action GetAction()
{
if (targetObject == null || string.IsNullOrWhiteSpace(methodName)) return null;
if (cachedAction == null)
{
if (targetObject == null)
throw new ArgumentNullException(nameof(targetObject), "Target Object is null!");
if (string.IsNullOrWhiteSpace(methodName))
throw new ArgumentNullException(nameof(methodName), "Target Method is null!");
MethodInfo info = targetObject
.GetType()
.GetMethods(SuitableMethodsFlags)
@@ -46,7 +41,9 @@ namespace SerializableFunc.Runtime
if (info == null)
{
throw new MissingMethodException($"Object \"{targetObject.name}\" is missing target void method: {methodName}");
throw new MissingMethodException(
$"Object \"{targetObject.name}\" is missing target void method: {methodName}"
);
}
cachedAction = (Action)Delegate.CreateDelegate(typeof(Action), targetObject, methodName);

View File

@@ -2,20 +2,19 @@ using System;
using System.Linq;
using System.Reflection;
using UnityEngine;
using Object = UnityEngine.Object;
namespace SerializableFunc.Runtime
{
[Serializable]
public abstract class SerializableFuncBase<TFuncType>
where TFuncType : Delegate
public abstract class SerializableFuncBase<TFuncType> where TFuncType : Delegate
{
[SerializeField] protected Object targetObject;
[SerializeField] protected UnityEngine.Object targetObject;
[SerializeField] protected string methodName;
private TFuncType func;
private static BindingFlags SuitableMethodsFlags = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance;
private static BindingFlags SuitableMethodsFlags =
BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance;
public UnityEngine.Object TargetObject
{
@@ -31,18 +30,9 @@ namespace SerializableFunc.Runtime
protected TFuncType GetReturnedFunc()
{
if (targetObject == null || string.IsNullOrWhiteSpace(methodName)) return null;
if (func == null)
{
if (targetObject == null)
{
throw new ArgumentNullException("Target Object is null!");
}
if (string.IsNullOrWhiteSpace(methodName))
{
throw new ArgumentNullException("Target Method is null!");
}
Type funcType = typeof(TFuncType);
MethodInfo info = targetObject
@@ -52,7 +42,9 @@ namespace SerializableFunc.Runtime
if (info == null)
{
throw new MissingMethodException($"Object \"{targetObject.name}\" is missing target method: {methodName}");
throw new MissingMethodException(
$"Object \"{targetObject.name}\" is missing target method: {methodName}"
);
}
func = (TFuncType)Delegate.CreateDelegate(funcType, targetObject, methodName);