diff --git a/BinaryObjectScanner/FileType/Executable.cs b/BinaryObjectScanner/FileType/Executable.cs index 82ea7c09..959a9392 100644 --- a/BinaryObjectScanner/FileType/Executable.cs +++ b/BinaryObjectScanner/FileType/Executable.cs @@ -529,7 +529,7 @@ namespace BinaryObjectScanner.FileType return protections; } -#endregion + #endregion #region Initializers @@ -537,29 +537,40 @@ namespace BinaryObjectScanner.FileType /// Initialize all implementations of a type /// private static IEnumerable? InitCheckClasses() => - InitCheckClasses(typeof(Handler).Assembly) ?? []; + InitCheckClasses(Assembly.GetExecutingAssembly()) ?? []; /// /// Initialize all implementations of a type /// private static IEnumerable? InitCheckClasses(Assembly assembly) { - List types = []; + List classTypes = []; + + // If not all types can be loaded, use the ones that could be + List assemblyTypes = []; try { - foreach (Type type in assembly.GetTypes()) - { - if (type.IsClass && type.GetInterface(typeof(T).Name) != null) - { - var instance = (T?)Activator.CreateInstance(type); - if (instance != null) - types.Add(instance); - } - } + assemblyTypes = assembly.GetTypes().ToList(); + } + catch (ReflectionTypeLoadException rtle) + { + assemblyTypes = rtle.Types.Where(t => t != null)!.ToList(); } - catch { } - return types; + // Loop through all types + foreach (Type type in assemblyTypes) + { + // If the type isn't a class or doesn't implement the interface + if (!type.IsClass || type.GetInterface(typeof(T).Name) == null) + continue; + + // Try to create a concrete instance of the type + var instance = (T?)Activator.CreateInstance(type); + if (instance != null) + classTypes.Add(instance); + } + + return classTypes; } #endregion diff --git a/BinaryObjectScanner/Handler.cs b/BinaryObjectScanner/Handler.cs index ab368782..6e588f57 100644 --- a/BinaryObjectScanner/Handler.cs +++ b/BinaryObjectScanner/Handler.cs @@ -204,29 +204,40 @@ namespace BinaryObjectScanner /// Initialize all implementations of a type /// private static IEnumerable InitCheckClasses() => - InitCheckClasses(typeof(Handler).Assembly); + InitCheckClasses(Assembly.GetExecutingAssembly()); /// /// Initialize all implementations of a type /// private static IEnumerable InitCheckClasses(Assembly assembly) { - List types = []; + List classTypes = []; + + // If not all types can be loaded, use the ones that could be + List assemblyTypes = []; try { - foreach (Type type in assembly.GetTypes()) - { - if (type.IsClass && type.GetInterface(typeof(T).Name) != null) - { - var instance = (T?)Activator.CreateInstance(type); - if (instance != null) - types.Add(instance); - } - } + assemblyTypes = assembly.GetTypes().ToList(); + } + catch (ReflectionTypeLoadException rtle) + { + assemblyTypes = rtle.Types.Where(t => t != null)!.ToList(); } - catch { } - return types; + // Loop through all types + foreach (Type type in assemblyTypes) + { + // If the type isn't a class or doesn't implement the interface + if (!type.IsClass || type.GetInterface(typeof(T).Name) == null) + continue; + + // Try to create a concrete instance of the type + var instance = (T?)Activator.CreateInstance(type); + if (instance != null) + classTypes.Add(instance); + } + + return classTypes; } #endregion