implement function to find a Type given a particular name

This commit is contained in:
Michael Becker 2020-02-29 17:59:41 -05:00
parent c565e8203c
commit 5e45a6d912
No known key found for this signature in database
GPG Key ID: 506F54899E2BFED7

View File

@ -26,6 +26,45 @@ namespace MBS.Framework
{ {
public class Reflection public class Reflection
{ {
private static Dictionary<string, Type> TypesByName = new Dictionary<string, Type>();
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; private static Assembly[] mvarAvailableAssemblies = null;
public static Assembly[] GetAvailableAssemblies() public static Assembly[] GetAvailableAssemblies()
{ {