From ea125c7c8fe2b6e34d7c39d33b94edc567135d9e Mon Sep 17 00:00:00 2001 From: Alexander Filippov Date: Tue, 4 Nov 2025 06:59:22 +0100 Subject: [PATCH] Replace exceptions with null checks to avoid event errors. --- Runtime/SerializableAction.cs | 11 ++++------- Runtime/SerializableFuncBase.cs | 24 ++++++++---------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/Runtime/SerializableAction.cs b/Runtime/SerializableAction.cs index 661a5fa..216fc78 100644 --- a/Runtime/SerializableAction.cs +++ b/Runtime/SerializableAction.cs @@ -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); diff --git a/Runtime/SerializableFuncBase.cs b/Runtime/SerializableFuncBase.cs index 744b2f1..7a8fcab 100644 --- a/Runtime/SerializableFuncBase.cs +++ b/Runtime/SerializableFuncBase.cs @@ -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 - where TFuncType : Delegate + public abstract class SerializableFuncBase 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);