From 2da999008c89275ea45fee68f54256eb1ee42c13 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Mon, 14 Mar 2022 21:03:17 -0400 Subject: [PATCH] bring in useful InvokeMethod functionality from MBS.Framework.UI --- MBS.Framework/Reflection.cs | 47 +++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/MBS.Framework/Reflection.cs b/MBS.Framework/Reflection.cs index 5fbb741..d1df0bc 100644 --- a/MBS.Framework/Reflection.cs +++ b/MBS.Framework/Reflection.cs @@ -24,7 +24,7 @@ using System.Reflection; namespace MBS.Framework { - public class Reflection + public static class Reflection { public class ManifestResourceStream { @@ -168,10 +168,10 @@ namespace MBS.Framework catch (ReflectionTypeLoadException ex) { Console.Error.WriteLine("ReflectionTypeLoadException(" + ex.LoaderExceptions.Length.ToString() + "): " + asm.FullName); - for (int i = 0; i < ex.LoaderExceptions.Length; i++) + for (int i = 0; i < ex.LoaderExceptions.Length; i++) { - Console.Error.WriteLine("\t" + ex.LoaderExceptions[i].Message); - Console.Error.WriteLine(); + Console.Error.WriteLine("\t" + ex.LoaderExceptions[i].Message); + Console.Error.WriteLine(); } types1 = ex.Types; @@ -272,5 +272,44 @@ namespace MBS.Framework return default(T); } + + public static void InvokeMethod(object obj, string meth, params object[] parms) + { + if (obj == null) + { + Console.WriteLine("Engine::InvokeMethod: obj is null"); + return; + } + + Type t = obj.GetType(); + System.Reflection.MethodInfo mi = t.GetMethod(meth, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); + if (mi != null) + { + mi.Invoke(obj, parms); + } + else + { + Console.WriteLine("Engine::InvokeMethod: method not found '" + meth + "' on '" + t.FullName + "'"); + } + } + public static void InvokeMethod(Type type, string meth, params object[] parms) + { + if (type == null) + { + Console.WriteLine("Engine::InvokeMethod: obj is null"); + return; + } + + Type t = type; + System.Reflection.MethodInfo mi = t.GetMethod(meth, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static); + if (mi != null) + { + mi.Invoke(null, parms); + } + else + { + Console.WriteLine("Engine::InvokeMethod: static method not found '" + meth + "' on '" + t.FullName + "'"); + } + } } }