From 5e45a6d912fc595dee46e752118c3045564c8aab Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sat, 29 Feb 2020 17:59:41 -0500 Subject: [PATCH] implement function to find a Type given a particular name --- MBS.Framework/Reflection.cs | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/MBS.Framework/Reflection.cs b/MBS.Framework/Reflection.cs index d1d2b88..0ffe98f 100644 --- a/MBS.Framework/Reflection.cs +++ b/MBS.Framework/Reflection.cs @@ -26,6 +26,45 @@ namespace MBS.Framework { public class Reflection { + private static Dictionary TypesByName = new Dictionary(); + public static Type FindType(string TypeName) + { + if (!TypesByName.ContainsKey(TypeName)) + { + Assembly[] asms = GetAvailableAssemblies(); + bool found = false; + for (int i = 0; i < asms.Length; i++) + { + Type[] types = null; + try + { + types = asms[i].GetTypes(); + } + catch (ReflectionTypeLoadException ex) + { + Console.Error.WriteLine("ReflectionTypeLoadException(" + ex.LoaderExceptions.Length.ToString() + "): " + asms[i].FullName); + Console.Error.WriteLine(ex.Message); + + types = ex.Types; + } + for (int j = 0; j < types.Length; j++) + { + if (types[j] == null) continue; + if (types[j].FullName == TypeName) + { + TypesByName.Add(TypeName, types[j]); + found = true; + break; + } + } + if (found) break; + } + if (!found) return null; + } + return TypesByName[TypeName]; + } + + private static Assembly[] mvarAvailableAssemblies = null; public static Assembly[] GetAvailableAssemblies() {