diff --git a/BinaryObjectScanner/Data/StaticChecks.cs b/BinaryObjectScanner/Data/StaticChecks.cs index 1b7f0af9..23045896 100644 --- a/BinaryObjectScanner/Data/StaticChecks.cs +++ b/BinaryObjectScanner/Data/StaticChecks.cs @@ -1,4 +1,7 @@ +using System; using System.Collections.Generic; +using System.Linq; +using System.Reflection; using BinaryObjectScanner.Interfaces; using SabreTools.Serialization.Wrappers; @@ -15,7 +18,7 @@ namespace BinaryObjectScanner.Data { get { - contentCheckClasses ??= Factory.InitCheckClasses(); + contentCheckClasses ??= InitCheckClasses(); return contentCheckClasses ?? []; } } @@ -27,7 +30,7 @@ namespace BinaryObjectScanner.Data { get { - linearExecutableCheckClasses ??= Factory.InitCheckClasses>(); + linearExecutableCheckClasses ??= InitCheckClasses>(); return linearExecutableCheckClasses ?? []; } } @@ -39,7 +42,7 @@ namespace BinaryObjectScanner.Data { get { - msdosExecutableCheckClasses ??= Factory.InitCheckClasses>(); + msdosExecutableCheckClasses ??= InitCheckClasses>(); return msdosExecutableCheckClasses ?? []; } } @@ -51,7 +54,7 @@ namespace BinaryObjectScanner.Data { get { - newExecutableCheckClasses ??= Factory.InitCheckClasses>(); + newExecutableCheckClasses ??= InitCheckClasses>(); return newExecutableCheckClasses ?? []; } } @@ -63,7 +66,7 @@ namespace BinaryObjectScanner.Data { get { - pathCheckClasses ??= Factory.InitCheckClasses(); + pathCheckClasses ??= InitCheckClasses(); return pathCheckClasses ?? []; } } @@ -75,7 +78,7 @@ namespace BinaryObjectScanner.Data { get { - portableExecutableCheckClasses ??= Factory.InitCheckClasses>(); + portableExecutableCheckClasses ??= InitCheckClasses>(); return portableExecutableCheckClasses ?? []; } } @@ -115,5 +118,61 @@ namespace BinaryObjectScanner.Data private static List>? portableExecutableCheckClasses; #endregion + + /// + /// Initialize all implementations of a type + /// + private static List? InitCheckClasses() => + InitCheckClasses(Assembly.GetExecutingAssembly()) ?? []; + + /// + /// Initialize all implementations of a type + /// + private static List? InitCheckClasses(Assembly assembly) + { + List classTypes = []; + + // If not all types can be loaded, use the ones that could be + List assemblyTypes = []; + try + { + assemblyTypes = assembly.GetTypes().ToList(); + } + catch (ReflectionTypeLoadException rtle) + { + assemblyTypes = rtle.Types.Where(t => t != null)!.ToList(); + } + + // Get information from the type param + string interfaceName = typeof(T)!.FullName; + + // Loop through all types + foreach (Type type in assemblyTypes) + { + // If the type isn't a class + if (!type.IsClass) + continue; + + // If the type isn't a class or doesn't implement the interface + bool interfaceFound = false; + foreach (var ii in type.GetInterfaces()) + { + if (ii.FullName != interfaceName) + continue; + + interfaceFound = true; + break; + } + if (!interfaceFound) + continue; + + // Try to create a concrete instance of the type + var instance = (T?)Activator.CreateInstance(type); + if (instance != null) + classTypes.Add(instance); + } + + return classTypes; + } } } \ No newline at end of file diff --git a/BinaryObjectScanner/Data/Factory.cs b/BinaryObjectScanner/Factory.cs similarity index 61% rename from BinaryObjectScanner/Data/Factory.cs rename to BinaryObjectScanner/Factory.cs index 0b33f360..4c70ae60 100644 --- a/BinaryObjectScanner/Data/Factory.cs +++ b/BinaryObjectScanner/Factory.cs @@ -1,11 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using BinaryObjectScanner.Interfaces; +using BinaryObjectScanner.Interfaces; using SabreTools.Serialization.Wrappers; -namespace BinaryObjectScanner.Data +namespace BinaryObjectScanner { public static class Factory { @@ -72,61 +68,5 @@ namespace BinaryObjectScanner.Data _ => null, }; } - - /// - /// Initialize all implementations of a type - /// - public static List? InitCheckClasses() => - InitCheckClasses(Assembly.GetExecutingAssembly()) ?? []; - - /// - /// Initialize all implementations of a type - /// - public static List? InitCheckClasses(Assembly assembly) - { - List classTypes = []; - - // If not all types can be loaded, use the ones that could be - List assemblyTypes = []; - try - { - assemblyTypes = assembly.GetTypes().ToList(); - } - catch (ReflectionTypeLoadException rtle) - { - assemblyTypes = rtle.Types.Where(t => t != null)!.ToList(); - } - - // Get information from the type param - string interfaceName = typeof(T)!.FullName; - - // Loop through all types - foreach (Type type in assemblyTypes) - { - // If the type isn't a class - if (!type.IsClass) - continue; - - // If the type isn't a class or doesn't implement the interface - bool interfaceFound = false; - foreach (var ii in type.GetInterfaces()) - { - if (ii.FullName != interfaceName) - continue; - - interfaceFound = true; - break; - } - if (!interfaceFound) - continue; - - // Try to create a concrete instance of the type - var instance = (T?)Activator.CreateInstance(type); - if (instance != null) - classTypes.Add(instance); - } - - return classTypes; - } } }