Skip to content

Commit 14dae3d

Browse files
committed
Rearrange code to be safer and more efficient
1 parent 80cd7df commit 14dae3d

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

BinaryObjectScanner/Data/StaticChecks.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,26 @@ public static List<IExecutableCheck<PortableExecutable>> PortableExecutableCheck
129129
/// <summary>
130130
/// Initialize all implementations of a type
131131
/// </summary>
132-
private static List<T>? InitCheckClasses<T>(Assembly assembly)
132+
private static List<T> InitCheckClasses<T>(Assembly assembly)
133133
{
134-
List<T> classTypes = [];
135-
136134
// If not all types can be loaded, use the ones that could be
137-
Type?[] assemblyTypes = [];
135+
Type?[] assemblyTypes;
138136
try
139137
{
140138
assemblyTypes = assembly.GetTypes();
141139
}
142140
catch (ReflectionTypeLoadException rtle)
143141
{
144-
assemblyTypes = [.. rtle!.Types!];
142+
assemblyTypes = rtle.Types ?? [];
145143
}
146144

147145
// Get information from the type param
148-
string interfaceName = typeof(T)!.FullName!;
146+
string? interfaceName = typeof(T).FullName;
147+
if (interfaceName == null)
148+
return [];
149149

150150
// Loop through all types
151+
List<T> classTypes = [];
151152
foreach (Type? type in assemblyTypes)
152153
{
153154
// Skip invalid types
@@ -159,16 +160,8 @@ public static List<IExecutableCheck<PortableExecutable>> PortableExecutableCheck
159160
continue;
160161

161162
// If the type isn't a class or doesn't implement the interface
162-
bool interfaceFound = false;
163-
foreach (var ii in type.GetInterfaces())
164-
{
165-
if (ii.FullName != interfaceName)
166-
continue;
167-
168-
interfaceFound = true;
169-
break;
170-
}
171-
if (!interfaceFound)
163+
var interfaces = Array.ConvertAll(type.GetInterfaces(), i => i.FullName);
164+
if (!Array.Exists(interfaces, i => i == interfaceName))
172165
continue;
173166

174167
// Try to create a concrete instance of the type

0 commit comments

Comments
 (0)