From 14dae3dd73fdb0e1da2416849c81ebc29a99e300 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sat, 20 Sep 2025 16:50:41 -0400 Subject: [PATCH] Rearrange code to be safer and more efficient --- BinaryObjectScanner/Data/StaticChecks.cs | 25 +++++++++--------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/BinaryObjectScanner/Data/StaticChecks.cs b/BinaryObjectScanner/Data/StaticChecks.cs index f085763b..17ba7e67 100644 --- a/BinaryObjectScanner/Data/StaticChecks.cs +++ b/BinaryObjectScanner/Data/StaticChecks.cs @@ -129,25 +129,26 @@ namespace BinaryObjectScanner.Data /// /// Initialize all implementations of a type /// - private static List? InitCheckClasses(Assembly assembly) + private static List InitCheckClasses(Assembly assembly) { - List classTypes = []; - // If not all types can be loaded, use the ones that could be - Type?[] assemblyTypes = []; + Type?[] assemblyTypes; try { assemblyTypes = assembly.GetTypes(); } catch (ReflectionTypeLoadException rtle) { - assemblyTypes = [.. rtle!.Types!]; + assemblyTypes = rtle.Types ?? []; } // Get information from the type param - string interfaceName = typeof(T)!.FullName!; + string? interfaceName = typeof(T).FullName; + if (interfaceName == null) + return []; // Loop through all types + List classTypes = []; foreach (Type? type in assemblyTypes) { // Skip invalid types @@ -159,16 +160,8 @@ namespace BinaryObjectScanner.Data 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) + var interfaces = Array.ConvertAll(type.GetInterfaces(), i => i.FullName); + if (!Array.Exists(interfaces, i => i == interfaceName)) continue; // Try to create a concrete instance of the type