diff --git a/BurnOutSharp/FileType/Executable.cs b/BurnOutSharp/FileType/Executable.cs index 8d025381..d1c7047d 100644 --- a/BurnOutSharp/FileType/Executable.cs +++ b/BurnOutSharp/FileType/Executable.cs @@ -12,7 +12,7 @@ namespace BurnOutSharp.FileType /// /// Cache for all IContentCheck types /// - private static IEnumerable contentCheckClasses = null; + private static IEnumerable contentCheckClasses = InitContentCheckClasses(); /// public bool ShouldScan(byte[] magic) @@ -77,30 +77,22 @@ namespace BurnOutSharp.FileType // Files can be protected in multiple ways var protections = new Dictionary>(); - // Get all IContentCheck implementations - if (contentCheckClasses == null) - { - contentCheckClasses = Assembly.GetExecutingAssembly().GetTypes() - .Where(t => t.IsClass && t.GetInterface("IContentCheck") != null); - } - // Iterate through all content checks foreach (var contentCheckClass in contentCheckClasses) { - IContentCheck contentCheck = Activator.CreateInstance(contentCheckClass) as IContentCheck; - string protection = contentCheck.CheckContents(file, fileContent, scanner.IncludePosition); + string protection = contentCheckClass.CheckContents(file, fileContent, scanner.IncludePosition); // If we have a valid content check based on settings - if (!contentCheckClass.Namespace.ToLowerInvariant().Contains("packertype") || scanner.ScanPackers) + if (!contentCheckClass.GetType().Namespace.ToLowerInvariant().Contains("packertype") || scanner.ScanPackers) { if (!string.IsNullOrWhiteSpace(protection)) Utilities.AppendToDictionary(protections, file, protection); } // If we have an IScannable implementation - if (contentCheckClass.GetInterface("IScannable") != null) + if (contentCheckClass is IScannable) { - IScannable scannable = Activator.CreateInstance(contentCheckClass) as IScannable; + IScannable scannable = contentCheckClass as IScannable; if (file != null && !string.IsNullOrEmpty(protection)) { var subProtections = scannable.Scan(scanner, null, file); @@ -112,5 +104,15 @@ namespace BurnOutSharp.FileType return protections; } + + /// + /// Initialize all IContentCheck implementations + /// + private static IEnumerable InitContentCheckClasses() + { + return Assembly.GetExecutingAssembly().GetTypes() + .Where(t => t.IsClass && t.GetInterface(nameof(IContentCheck)) != null) + .Select(t => Activator.CreateInstance(t) as IContentCheck); + } } } diff --git a/BurnOutSharp/Scanner.cs b/BurnOutSharp/Scanner.cs index 443796d5..7fde7325 100644 --- a/BurnOutSharp/Scanner.cs +++ b/BurnOutSharp/Scanner.cs @@ -37,7 +37,7 @@ namespace BurnOutSharp /// /// Cache for all IPathCheck types /// - private static IEnumerable pathCheckClasses = null; + private static IEnumerable pathCheckClasses = InitPathCheckClasses(); /// /// Constructor @@ -186,14 +186,10 @@ namespace BurnOutSharp // Create an empty list for protections List protections = new List(); - // Initialize the needed classes - InitPathCheckClasses(); - // Iterate through all path checks foreach (var pathCheckClass in pathCheckClasses) { - IPathCheck pathCheck = Activator.CreateInstance(pathCheckClass) as IPathCheck; - string protection = pathCheck.CheckDirectoryPath(path, files); + string protection = pathCheckClass.CheckDirectoryPath(path, files); if (!string.IsNullOrWhiteSpace(protection)) protections.Add(protection); } @@ -215,14 +211,10 @@ namespace BurnOutSharp // Create an empty list for protections List protections = new List(); - // Initialize the needed classes - InitPathCheckClasses(); - // Iterate through all path checks foreach (var pathCheckClass in pathCheckClasses) { - IPathCheck pathCheck = Activator.CreateInstance(pathCheckClass) as IPathCheck; - string protection = pathCheck.CheckFilePath(path); + string protection = pathCheckClass.CheckFilePath(path); if (!string.IsNullOrWhiteSpace(protection)) protections.Add(protection); } @@ -407,13 +399,11 @@ namespace BurnOutSharp /// /// Initialize all IPathCheck implementations /// - private static void InitPathCheckClasses() + private static IEnumerable InitPathCheckClasses() { - if (pathCheckClasses == null) - { - pathCheckClasses = Assembly.GetExecutingAssembly().GetTypes() - .Where(t => t.IsClass && t.GetInterface("IPathCheck") != null); - } + return Assembly.GetExecutingAssembly().GetTypes() + .Where(t => t.IsClass && t.GetInterface(nameof(IPathCheck)) != null) + .Select(t => Activator.CreateInstance(t) as IPathCheck); } } }