refactor common reflection stuff from UniversalEditor into MBS.Framework

This commit is contained in:
Michael Becker 2019-11-29 18:55:10 -05:00
parent b0f50b483d
commit af7b8914ec
No known key found for this signature in database
GPG Key ID: 389DFF5D73781A12
5 changed files with 47 additions and 175 deletions

View File

@ -30,62 +30,13 @@ namespace UniversalEditor.Common
return a.Type.FullName.CompareTo(b.Type.FullName);
}
private static Type[] mvarAvailableTypes = null;
public static Type[] GetAvailableTypes(Type[] inheritsFrom = null)
{
if (mvarAvailableTypes == null)
{
List<Type> types = new List<Type>();
Assembly[] asms = GetAvailableAssemblies();
for (int iAsm = 0; iAsm < asms.Length; iAsm++)
{
Assembly asm = asms[iAsm];
Type[] types1 = null;
try
{
types1 = asm.GetTypes();
}
catch (ReflectionTypeLoadException ex)
{
Console.Error.WriteLine("ReflectionTypeLoadException(" + ex.LoaderExceptions.Length.ToString() + "): " + asm.FullName);
Console.Error.WriteLine(ex.Message);
types1 = ex.Types;
}
if (types1 == null) continue;
for (int jTyp = 0; jTyp < types1.Length; jTyp++)
{
if (types1[jTyp] == null) continue;
types.Add(types1[jTyp]);
}
}
mvarAvailableTypes = types.ToArray();
}
if (inheritsFrom != null)
{
List<Type> retval = new List<Type>();
for (int iTyp = 0; iTyp < mvarAvailableTypes.Length; iTyp++)
{
for (int jInh = 0; jInh < inheritsFrom.Length; jInh++)
{
if (mvarAvailableTypes[iTyp].IsSubclassOf(inheritsFrom[jInh])) retval.Add(mvarAvailableTypes[iTyp]);
}
}
return retval.ToArray();
}
return mvarAvailableTypes;
}
#region Initialization
private static bool mvarInitialized = false;
private static void Initialize()
{
if (mvarInitialized) return;
Type[] types = GetAvailableTypes();
Type[] types = MBS.Framework.Reflection.GetAvailableTypes();
#region Initializing Object Models
List<AccessorReference> listAccessors = new List<AccessorReference>();
@ -636,48 +587,6 @@ namespace UniversalEditor.Common
return null;
}
#endregion
#region Assemblies
private static Assembly[] mvarAvailableAssemblies = null;
public static Assembly[] GetAvailableAssemblies()
{
if (mvarAvailableAssemblies == null)
{
List<Assembly> list = new List<Assembly>();
List<string> asmdirs = new List<string>();
string dir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
asmdirs.Add(dir);
asmdirs.Add(dir + System.IO.Path.DirectorySeparatorChar.ToString() + "Plugins");
foreach (string asmdir in asmdirs)
{
if (!System.IO.Directory.Exists(asmdir)) continue;
string[] FileNamesEXE = System.IO.Directory.GetFiles(asmdir, "*.exe", System.IO.SearchOption.TopDirectoryOnly);
string[] FileNamesDLL = System.IO.Directory.GetFiles(asmdir, "*.dll", System.IO.SearchOption.TopDirectoryOnly);
string[] FileNames = new string[FileNamesEXE.Length + FileNamesDLL.Length];
Array.Copy(FileNamesEXE, 0, FileNames, 0, FileNamesEXE.Length);
Array.Copy(FileNamesDLL, 0, FileNames, FileNamesEXE.Length, FileNamesDLL.Length);
foreach (string FileName in FileNames)
{
try
{
Assembly asm = Assembly.LoadFile(FileName);
list.Add(asm);
}
catch
{
}
}
}
mvarAvailableAssemblies = list.ToArray();
}
return mvarAvailableAssemblies;
}
#endregion
private static DocumentTemplate[] mvarAvailableDocumentTemplates = null;
public static DocumentTemplate[] GetAvailableDocumentTemplates()

View File

@ -945,7 +945,7 @@ namespace UniversalEditor.DataFormats.UEPackage
{
CustomOption co = null;
Type[] tCustomOptions = UniversalEditor.Common.Reflection.GetAvailableTypes(new Type[] { typeof(CustomOption) });
Type[] tCustomOptions = MBS.Framework.Reflection.GetAvailableTypes(new Type[] { typeof(CustomOption) });
foreach (Type tCustomOption in tCustomOptions)
{
if (tCustomOption.Name == tag.FullName)

View File

@ -85,10 +85,10 @@ namespace UniversalEditor
if (_dict == null)
{
_dict = new Dictionary<Guid, ProjectTaskActionReference>();
Type[] types = Common.Reflection.GetAvailableTypes();
Type[] types = MBS.Framework.Reflection.GetAvailableTypes(new Type[] { typeof(ProjectTaskAction) });
foreach (Type type in types)
{
if (!type.IsAbstract && type.IsSubclassOf(typeof(ProjectTaskAction)))
if (!type.IsAbstract)
{
ProjectTaskAction action = (ProjectTaskAction)type.Assembly.CreateInstance(type.FullName);
ProjectTaskActionReference actionref = action.MakeReference();

View File

@ -30,8 +30,23 @@ namespace UniversalEditor.Printing
public static PrintHandlerReference[] GetAvailablePrintHandlers()
{
if (_phrs == null)
Initialize();
{
List<PrintHandlerReference> list = new List<PrintHandlerReference>();
Type[] types = MBS.Framework.Reflection.GetAvailableTypes(new Type[] { typeof(PrintHandler) });
foreach (Type type in types)
{
if (type == null) continue;
if (type.IsSubclassOf(typeof(PrintHandler)))
{
PrintHandler ph = (type.Assembly.CreateInstance(type.FullName) as PrintHandler);
PrintHandlerReference phr = ph.MakeReference();
list.Add(phr);
}
}
_phrs = list.ToArray();
}
return _phrs;
}
@ -46,41 +61,5 @@ namespace UniversalEditor.Printing
}
return list.ToArray();
}
private static Assembly[] _asms = null;
private static void Initialize()
{
if (_asms == null)
{
List<PrintHandlerReference> list = new List<PrintHandlerReference>();
_asms = UniversalEditor.Common.Reflection.GetAvailableAssemblies();
foreach (Assembly asm in _asms)
{
Type[] types = null;
try
{
types = asm.GetTypes();
}
catch (ReflectionTypeLoadException ex)
{
types = ex.Types;
}
foreach (Type type in types)
{
if (type == null) continue;
if (type.IsSubclassOf(typeof(PrintHandler)))
{
PrintHandler ph = (type.Assembly.CreateInstance(type.FullName) as PrintHandler);
PrintHandlerReference phr = ph.MakeReference();
list.Add(phr);
}
}
}
_phrs = list.ToArray();
}
}
}
}

View File

@ -12,60 +12,44 @@ namespace UniversalEditor.UserInterface.Common
{
private static void Initialize()
{
System.Reflection.Assembly[] asms = UniversalEditor.Common.Reflection.GetAvailableAssemblies();
List<EditorReference> listEditors = new List<EditorReference>();
if (mvarAvailableEditors == null)
{
foreach (System.Reflection.Assembly asm in asms)
Type[] types = MBS.Framework.Reflection.GetAvailableTypes(new Type[] { typeof(Editor) });
foreach (Type type in types)
{
Type[] types = null;
try
if (type == null) continue;
if (type.IsSubclassOf(typeof(Editor)))
{
types = asm.GetTypes();
}
catch (System.Reflection.ReflectionTypeLoadException ex)
{
Console.Error.WriteLine("ReflectionTypeLoadException(" + ex.LoaderExceptions.Length.ToString() + "): " + asm.FullName);
Console.Error.WriteLine(ex.Message);
#region Initializing Editors
Console.Write("loading editor '" + type.FullName + "'... ");
types = ex.Types;
}
foreach (Type type in types)
{
if (type == null) continue;
if (type.IsSubclassOf(typeof(Editor)))
try
{
#region Initializing Editors
Console.Write("loading editor '" + type.FullName + "'... ");
// TODO: see if there is a way we can MakeReference() without having to create all the UI
// components of the IEditorImplementation
Editor editor = (type.Assembly.CreateInstance(type.FullName) as Editor);
listEditors.Add(editor.MakeReference());
try
{
// TODO: see if there is a way we can MakeReference() without having to create all the UI
// components of the IEditorImplementation
Editor editor = (type.Assembly.CreateInstance(type.FullName) as Editor);
listEditors.Add(editor.MakeReference());
Console.WriteLine("SUCCESS!");
}
catch (System.Reflection.TargetInvocationException ex)
{
Console.WriteLine("FAILURE!");
Console.WriteLine("binding error: " + ex.InnerException.Message);
}
catch (Exception ex)
{
Console.WriteLine("FAILURE!");
Console.WriteLine("error while loading editor '" + type.FullName + "': " + ex.Message);
}
Console.WriteLine("SUCCESS!");
}
catch (System.Reflection.TargetInvocationException ex)
{
Console.WriteLine("FAILURE!");
Console.WriteLine("binding error: " + ex.InnerException.Message);
}
catch (Exception ex)
{
Console.WriteLine("FAILURE!");
Console.WriteLine("error while loading editor '" + type.FullName + "': " + ex.Message);
}
#endregion
}
#endregion
}
}