From 69d2625828ab2cf5bbce80b0c834f387860f5a58 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Fri, 8 May 2026 03:43:45 +0330 Subject: [PATCH] last ... --- Helpers/XGenericHelper.cs | 78 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 Helpers/XGenericHelper.cs diff --git a/Helpers/XGenericHelper.cs b/Helpers/XGenericHelper.cs new file mode 100644 index 0000000..3b138d0 --- /dev/null +++ b/Helpers/XGenericHelper.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using xCommons.Extensions; + +namespace xCommons.Helpers +{ + public static class XGenericHelper + { + /// + /// Invoke a Generic Method of Specified Type ... + /// + /// + /// + /// + /// + /// + public static void InvokeGenericMethod( + this Type source, + string methodName, + Type runtimeType, + BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic, + params object[] args + ) + { + // + var overLoads = source.GetGenericMethodInfo( + methodName: methodName, + bindingAttr: bindingAttr + ); + if (!overLoads.HasChild()) + { + return; + } + + // + var method = overLoads.ElementAt(0); + MethodInfo genericMethod = method.MakeGenericMethod(runtimeType); + + // + var gParams = new object[] + { + source + } + .Concat(args) + .ToArray(); + + // + genericMethod.Invoke(null, gParams); + } + + /// + /// Extract a Generic Method's Overloads ... + /// + /// + /// + /// + /// + public static List GetGenericMethodInfo( + this Type source, + string methodName, + BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic + ) + { + // + var result = source + .GetMethods(bindingAttr) + .Where(mi => + mi.Name == methodName && + mi.IsGenericMethodDefinition) + .ToList(); + + // + return result; + } + } +} \ No newline at end of file