From 9882febede5d81fb6d80d73a03cfeee27d4d36d8 Mon Sep 17 00:00:00 2001 From: Drew Tingen Date: Tue, 7 Jul 2020 21:30:07 -0400 Subject: [PATCH] feat: Adding CallMethod reflection extensions --- .../Extensions/ReflectionExtensions.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/ICD.Common.Utils/Extensions/ReflectionExtensions.cs b/ICD.Common.Utils/Extensions/ReflectionExtensions.cs index 1515cfe..f41a53c 100644 --- a/ICD.Common.Utils/Extensions/ReflectionExtensions.cs +++ b/ICD.Common.Utils/Extensions/ReflectionExtensions.cs @@ -195,6 +195,41 @@ namespace ICD.Common.Utils.Extensions value = currentObject; return true; } + + public static bool CallMethod([NotNull] this object extends, string methodName) + { + object value; + return CallMethod(extends, methodName, out value, new object[] { }); + } + + public static bool CallMethod([NotNull] this object extends, string methodName, [CanBeNull] out object value) + { + return CallMethod(extends, methodName, out value, new object[] { }); + } + + public static bool CallMethod([NotNull] this object extends, string methodName, [NotNull] params object[] parameters) + { + object value; + return CallMethod(extends, methodName, out value, parameters); + } + + public static bool CallMethod([NotNull] this object extends, string methodName, [CanBeNull] out object value, [NotNull] params object[] parameters) + { + if (extends == null) + throw new ArgumentNullException(nameof(extends)); + if (parameters == null) + throw new ArgumentNullException(nameof(parameters)); + + value = false; + + MethodInfo method = extends.GetType().GetMethod(methodName); + if (method == null) + return false; + + value = method.Invoke(extends, parameters); + + return true; + } #endif ///