-
Notifications
You must be signed in to change notification settings - Fork 10
/
TypeLocator.cs
172 lines (152 loc) · 6.84 KB
/
TypeLocator.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) 2023 SceneGate
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
namespace Yarhl.Plugins;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
/// <summary>
/// Type locator. Find implementation of a given interface across loaded assemblies.
/// </summary>
public sealed class TypeLocator
{
private static readonly object LockObj = new();
private static TypeLocator? singleInstance;
/// <summary>
/// Initializes a new instance of the <see cref="TypeLocator"/> class.
/// </summary>
/// <param name="loadContext">The load context to search assemblies.</param>
public TypeLocator(AssemblyLoadContext loadContext)
{
LoadContext = loadContext;
}
/// <summary>
/// Initializes a new instance of the <see cref="TypeLocator"/> class.
/// </summary>
private TypeLocator()
{
LoadContext = AssemblyLoadContext.Default;
}
/// <summary>
/// Gets a singleton instance that use the default AssemblyLoadContext.
/// </summary>
/// <remarks>
/// <para>It initializes the type if needed on the first call.</para>
/// </remarks>
public static TypeLocator Default {
get {
if (singleInstance == null) {
lock (LockObj) {
singleInstance ??= new TypeLocator();
}
}
return singleInstance;
}
}
/// <summary>
/// Gets the assembly load context containing the assemblies to scan.
/// </summary>
/// <remarks>
/// Use the returned instance to load new assemblies.
/// </remarks>
public AssemblyLoadContext LoadContext { get; }
/// <summary>
/// Finds and returns a collection of types that implements the given
/// base type across all the loaded assemblies.
/// </summary>
/// <param name="baseType">The base type to find implementors.</param>
/// <returns>A collection of types implementing the base type.</returns>
public IEnumerable<TypeImplementationInfo> FindImplementationsOf(Type baseType)
{
ArgumentNullException.ThrowIfNull(baseType);
return LoadContext.Assemblies
.Where(a => !a.IsDynamic) // don't support iterating through types in .NET 6
.SelectMany(assembly => FindImplementationsOf(baseType, assembly));
}
/// <summary>
/// Finds and returns a collection of types that implements the given
/// base type in the assembly.
/// </summary>
/// <param name="baseType">The base type to find implementors.</param>
/// <param name="assembly">The assembly to scan.</param>
/// <returns>A collection of types implementing the base type.</returns>
public IEnumerable<TypeImplementationInfo> FindImplementationsOf(Type baseType, Assembly assembly)
{
ArgumentNullException.ThrowIfNull(baseType);
ArgumentNullException.ThrowIfNull(assembly);
if (baseType.IsGenericTypeDefinition) {
throw new ArgumentException(
"Generic type definition doesn't work on this method. " +
$"Use {nameof(FindImplementationsOfGeneric)} instead.",
nameof(baseType));
}
return assembly.ExportedTypes
.Where(baseType.IsAssignableFrom)
.Where(t => t.IsClass && !t.IsAbstract)
.Select(type => new TypeImplementationInfo(type.FullName!, type));
}
/// <summary>
/// Finds and returns a collection of types that implements the given
/// generic interface across all the loaded assemblies.
/// </summary>
/// <param name="baseType">The generic interface type to find implementors.</param>
/// <returns>A collection of types implementing the interface.</returns>
/// <remarks>
/// The list may contain several times the same if it implements the same interface
/// multiple types with different generic types.
/// </remarks>
public IEnumerable<GenericTypeImplementationInfo> FindImplementationsOfGeneric(Type baseType)
{
ArgumentNullException.ThrowIfNull(baseType);
return LoadContext.Assemblies
.Where(a => !a.IsDynamic) // don't support iterating through types in .NET 6
.SelectMany(assembly => FindImplementationsOfGeneric(baseType, assembly));
}
/// <summary>
/// Finds and returns a collection of types that implements the given
/// generic type in the assembly.
/// </summary>
/// <param name="baseType">The generic type to find implementors.</param>
/// <param name="assembly">The assembly to scan.</param>
/// <returns>A collection of types implementing the base type.</returns>
/// <remarks>
/// The list may contain several entries for the same implementation type
/// if it implements several type the generic with different parameters.
/// </remarks>
public IEnumerable<GenericTypeImplementationInfo> FindImplementationsOfGeneric(
Type baseType,
Assembly assembly)
{
ArgumentNullException.ThrowIfNull(baseType);
ArgumentNullException.ThrowIfNull(assembly);
bool ValidImplementationInterface(Type type) =>
type.IsGenericType
&& type.GetGenericTypeDefinition().IsEquivalentTo(baseType);
return assembly.ExportedTypes
.Where(t => t.IsClass && !t.IsAbstract)
.Where(t => Array.Exists(t.GetInterfaces(), ValidImplementationInterface))
.SelectMany(type => type.GetInterfaces() // A class may implement a generic interface multiple times
.Where(ValidImplementationInterface)
.Select(implementedInterface =>
new GenericTypeImplementationInfo(
type.FullName!,
type,
implementedInterface,
implementedInterface.GenericTypeArguments)));
}
}