File tree Expand file tree Collapse file tree 2 files changed +49
-27
lines changed Expand file tree Collapse file tree 2 files changed +49
-27
lines changed Original file line number Diff line number Diff line change @@ -529,37 +529,48 @@ public ConcurrentDictionary<IPortableExecutableCheck, string> RunPortableExecuta
529
529
return protections ;
530
530
}
531
531
532
- #endregion
532
+ #endregion
533
533
534
534
#region Initializers
535
535
536
536
/// <summary>
537
537
/// Initialize all implementations of a type
538
538
/// </summary>
539
539
private static IEnumerable < T > ? InitCheckClasses < T > ( ) =>
540
- InitCheckClasses < T > ( typeof ( Handler ) . Assembly ) ?? [ ] ;
540
+ InitCheckClasses < T > ( Assembly . GetExecutingAssembly ( ) ) ?? [ ] ;
541
541
542
542
/// <summary>
543
543
/// Initialize all implementations of a type
544
544
/// </summary>
545
545
private static IEnumerable < T > ? InitCheckClasses < T > ( Assembly assembly )
546
546
{
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 = [ ] ;
548
551
try
549
552
{
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 ) ;
559
571
}
560
- catch { }
561
572
562
- return types ;
573
+ return classTypes ;
563
574
}
564
575
565
576
#endregion
Original file line number Diff line number Diff line change @@ -204,29 +204,40 @@ public static ConcurrentDictionary<string, ConcurrentQueue<string>> HandlePathCh
204
204
/// Initialize all implementations of a type
205
205
/// </summary>
206
206
private static IEnumerable < T ? > InitCheckClasses < T > ( ) =>
207
- InitCheckClasses < T > ( typeof ( Handler ) . Assembly ) ;
207
+ InitCheckClasses < T > ( Assembly . GetExecutingAssembly ( ) ) ;
208
208
209
209
/// <summary>
210
210
/// Initialize all implementations of a type
211
211
/// </summary>
212
212
private static IEnumerable < T ? > InitCheckClasses < T > ( Assembly assembly )
213
213
{
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 = [ ] ;
215
218
try
216
219
{
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 ) ;
226
238
}
227
- catch { }
228
239
229
- return types ;
240
+ return classTypes ;
230
241
}
231
242
232
243
#endregion
You can’t perform that action at this time.
0 commit comments