Replace exceptions with null checks to avoid event errors.
This commit is contained in:
@@ -31,14 +31,9 @@ namespace SerializableFunc.Runtime
|
|||||||
|
|
||||||
private Action GetAction()
|
private Action GetAction()
|
||||||
{
|
{
|
||||||
|
if (targetObject == null || string.IsNullOrWhiteSpace(methodName)) return null;
|
||||||
if (cachedAction == 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
|
MethodInfo info = targetObject
|
||||||
.GetType()
|
.GetType()
|
||||||
.GetMethods(SuitableMethodsFlags)
|
.GetMethods(SuitableMethodsFlags)
|
||||||
@@ -46,7 +41,9 @@ namespace SerializableFunc.Runtime
|
|||||||
|
|
||||||
if (info == null)
|
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);
|
cachedAction = (Action)Delegate.CreateDelegate(typeof(Action), targetObject, methodName);
|
||||||
|
|||||||
@@ -2,20 +2,19 @@ using System;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using Object = UnityEngine.Object;
|
|
||||||
|
|
||||||
namespace SerializableFunc.Runtime
|
namespace SerializableFunc.Runtime
|
||||||
{
|
{
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public abstract class SerializableFuncBase<TFuncType>
|
public abstract class SerializableFuncBase<TFuncType> where TFuncType : Delegate
|
||||||
where TFuncType : Delegate
|
|
||||||
{
|
{
|
||||||
[SerializeField] protected Object targetObject;
|
[SerializeField] protected UnityEngine.Object targetObject;
|
||||||
[SerializeField] protected string methodName;
|
[SerializeField] protected string methodName;
|
||||||
|
|
||||||
private TFuncType func;
|
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
|
public UnityEngine.Object TargetObject
|
||||||
{
|
{
|
||||||
@@ -31,18 +30,9 @@ namespace SerializableFunc.Runtime
|
|||||||
|
|
||||||
protected TFuncType GetReturnedFunc()
|
protected TFuncType GetReturnedFunc()
|
||||||
{
|
{
|
||||||
|
if (targetObject == null || string.IsNullOrWhiteSpace(methodName)) return null;
|
||||||
if (func == 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);
|
Type funcType = typeof(TFuncType);
|
||||||
|
|
||||||
MethodInfo info = targetObject
|
MethodInfo info = targetObject
|
||||||
@@ -52,7 +42,9 @@ namespace SerializableFunc.Runtime
|
|||||||
|
|
||||||
if (info == null)
|
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);
|
func = (TFuncType)Delegate.CreateDelegate(funcType, targetObject, methodName);
|
||||||
|
|||||||
Reference in New Issue
Block a user