From 9e3ef360ac7fc1feb1ab2ec0f692076af418bedf Mon Sep 17 00:00:00 2001 From: Valentin Arthur Thomas Date: Sat, 14 Sep 2024 06:01:03 +0200 Subject: [PATCH] Add check returning type for 'get'reflection call in EBehaviour --- Exiled.API/Features/Core/Generic/EBehaviour.cs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/Exiled.API/Features/Core/Generic/EBehaviour.cs b/Exiled.API/Features/Core/Generic/EBehaviour.cs index 913d05f62c..7d95273864 100644 --- a/Exiled.API/Features/Core/Generic/EBehaviour.cs +++ b/Exiled.API/Features/Core/Generic/EBehaviour.cs @@ -59,18 +59,15 @@ protected EBehaviour() /// protected virtual void FindOwner() { - // @Nao T is a GameEntity, why not add in inisde of it the method a abstract. - // Or create an specific Interaface requesting to implement this method MethodInfo method = typeof(T).GetMethod("Get", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(GameObject) }, null); - if (method != null) - { - Owner = (T)method.Invoke(null, new object[] { Base }); - } - else - { + if (method == null) throw new MissingMethodException($"Method 'Get(GameObject)' not found in class '{typeof(T).Name}'."); - } + + if (typeof(T).IsAssignableFrom(method.ReturnType)) + throw new MissingMethodException($"Method 'Get(GameObject)' in class '{typeof(T).Name}' do not return an instance of {typeof(T).Name} but {method.ReturnType}."); + + Owner = (T)method.Invoke(null, new object[] { Base }); } ///