-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDynamicTypeLookupService.cs
50 lines (43 loc) · 2.65 KB
/
DynamicTypeLookupService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*==============================================================================================================================
| Author Ignia, LLC
| Client Ignia, LLC
| Project Topics Library
\=============================================================================================================================*/
namespace OnTopic.Lookup {
/*============================================================================================================================
| CLASS: DYNAMIC TYPE LOOKUP SERVICE
\---------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// The <see cref="DynamicTypeLookupService"/> will search all assemblies for <see cref="Type"/> instances that match a
/// predicate.
/// </summary>
public class DynamicTypeLookupService : StaticTypeLookupService {
/*==========================================================================================================================
| CONSTRUCTOR
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Establishes a new instance of a <see cref="DynamicTypeLookupService"/> based on a <paramref name="predicate"/> and,
/// optionally, a default <see cref="Type"/> object to return if none is specified.
/// </summary>
/// <param name="predicate">The search condition to use to identify target classes.</param>
public DynamicTypeLookupService(Func<Type, bool> predicate) : base() {
/*------------------------------------------------------------------------------------------------------------------------
| Find target classes
\-----------------------------------------------------------------------------------------------------------------------*/
var matchedTypes = AppDomain
.CurrentDomain
.GetAssemblies()
.Where(a => !a.FullName.StartsWith("Microsoft", StringComparison.Ordinal) && !a.FullName.StartsWith("System", StringComparison.Ordinal))
.SelectMany(t => t.GetTypes())
.Where(t => t.IsClass && predicate(t))
.OrderBy(t => t.Namespace?.StartsWith("OnTopic", StringComparison.Ordinal))
.ToList();
/*------------------------------------------------------------------------------------------------------------------------
| Populate collection
\-----------------------------------------------------------------------------------------------------------------------*/
foreach (var type in matchedTypes) {
TryAdd(type);
}
}
} //Class
} //Namespace