Skip to content

Commit de57851

Browse files
committed
Fix type retrieval for all build types
1 parent f1ec025 commit de57851

File tree

2 files changed

+49
-27
lines changed

2 files changed

+49
-27
lines changed

BinaryObjectScanner/FileType/Executable.cs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -529,37 +529,48 @@ public ConcurrentDictionary<IPortableExecutableCheck, string> RunPortableExecuta
529529
return protections;
530530
}
531531

532-
#endregion
532+
#endregion
533533

534534
#region Initializers
535535

536536
/// <summary>
537537
/// Initialize all implementations of a type
538538
/// </summary>
539539
private static IEnumerable<T>? InitCheckClasses<T>() =>
540-
InitCheckClasses<T>(typeof(Handler).Assembly) ?? [];
540+
InitCheckClasses<T>(Assembly.GetExecutingAssembly()) ?? [];
541541

542542
/// <summary>
543543
/// Initialize all implementations of a type
544544
/// </summary>
545545
private static IEnumerable<T>? InitCheckClasses<T>(Assembly assembly)
546546
{
547-
List<T> types = [];
547+
List<T> classTypes = [];
548+
549+
// If not all types can be loaded, use the ones that could be
550+
List<Type> assemblyTypes = [];
548551
try
549552
{
550-
foreach (Type type in assembly.GetTypes())
551-
{
552-
if (type.IsClass && type.GetInterface(typeof(T).Name) != null)
553-
{
554-
var instance = (T?)Activator.CreateInstance(type);
555-
if (instance != null)
556-
types.Add(instance);
557-
}
558-
}
553+
assemblyTypes = assembly.GetTypes().ToList<Type>();
554+
}
555+
catch (ReflectionTypeLoadException rtle)
556+
{
557+
assemblyTypes = rtle.Types.Where(t => t != null)!.ToList<Type>();
558+
}
559+
560+
// Loop through all types
561+
foreach (Type type in assemblyTypes)
562+
{
563+
// If the type isn't a class or doesn't implement the interface
564+
if (!type.IsClass || type.GetInterface(typeof(T).Name) == null)
565+
continue;
566+
567+
// Try to create a concrete instance of the type
568+
var instance = (T?)Activator.CreateInstance(type);
569+
if (instance != null)
570+
classTypes.Add(instance);
559571
}
560-
catch { }
561572

562-
return types;
573+
return classTypes;
563574
}
564575

565576
#endregion

BinaryObjectScanner/Handler.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -204,29 +204,40 @@ public static ConcurrentDictionary<string, ConcurrentQueue<string>> HandlePathCh
204204
/// Initialize all implementations of a type
205205
/// </summary>
206206
private static IEnumerable<T?> InitCheckClasses<T>() =>
207-
InitCheckClasses<T>(typeof(Handler).Assembly);
207+
InitCheckClasses<T>(Assembly.GetExecutingAssembly());
208208

209209
/// <summary>
210210
/// Initialize all implementations of a type
211211
/// </summary>
212212
private static IEnumerable<T?> InitCheckClasses<T>(Assembly assembly)
213213
{
214-
List<T?> types = [];
214+
List<T?> classTypes = [];
215+
216+
// If not all types can be loaded, use the ones that could be
217+
List<Type> assemblyTypes = [];
215218
try
216219
{
217-
foreach (Type type in assembly.GetTypes())
218-
{
219-
if (type.IsClass && type.GetInterface(typeof(T).Name) != null)
220-
{
221-
var instance = (T?)Activator.CreateInstance(type);
222-
if (instance != null)
223-
types.Add(instance);
224-
}
225-
}
220+
assemblyTypes = assembly.GetTypes().ToList<Type>();
221+
}
222+
catch (ReflectionTypeLoadException rtle)
223+
{
224+
assemblyTypes = rtle.Types.Where(t => t != null)!.ToList<Type>();
225+
}
226+
227+
// Loop through all types
228+
foreach (Type type in assemblyTypes)
229+
{
230+
// If the type isn't a class or doesn't implement the interface
231+
if (!type.IsClass || type.GetInterface(typeof(T).Name) == null)
232+
continue;
233+
234+
// Try to create a concrete instance of the type
235+
var instance = (T?)Activator.CreateInstance(type);
236+
if (instance != null)
237+
classTypes.Add(instance);
226238
}
227-
catch { }
228239

229-
return types;
240+
return classTypes;
230241
}
231242

232243
#endregion

0 commit comments

Comments
 (0)