-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
TypeLoader.cs
369 lines (327 loc) · 17.1 KB
/
TypeLoader.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Threading;
namespace Microsoft.Build.Shared
{
/// <summary>
/// This class is used to load types from their assemblies.
/// </summary>
internal class TypeLoader
{
#if FEATURE_ASSEMBLYLOADCONTEXT
/// <summary>
/// AssemblyContextLoader used to load DLLs outside of msbuild.exe directory
/// </summary>
private static readonly CoreClrAssemblyLoader s_coreClrAssemblyLoader;
#endif
/// <summary>
/// Cache to keep track of the assemblyLoadInfos based on a given type filter.
/// </summary>
private static ConcurrentDictionary<Func<Type, object, bool>, ConcurrentDictionary<AssemblyLoadInfo, AssemblyInfoToLoadedTypes>> s_cacheOfLoadedTypesByFilter = new ConcurrentDictionary<Func<Type, object, bool>, ConcurrentDictionary<AssemblyLoadInfo, AssemblyInfoToLoadedTypes>>();
/// <summary>
/// Cache to keep track of the assemblyLoadInfos based on a given type filter for assemblies which are to be loaded for reflectionOnlyLoads.
/// </summary>
private static ConcurrentDictionary<Func<Type, object, bool>, ConcurrentDictionary<AssemblyLoadInfo, AssemblyInfoToLoadedTypes>> s_cacheOfReflectionOnlyLoadedTypesByFilter = new ConcurrentDictionary<Func<Type, object, bool>, ConcurrentDictionary<AssemblyLoadInfo, AssemblyInfoToLoadedTypes>>();
/// <summary>
/// Type filter for this typeloader
/// </summary>
private Func<Type, object, bool> _isDesiredType;
#if FEATURE_ASSEMBLYLOADCONTEXT
static TypeLoader()
{
s_coreClrAssemblyLoader = new CoreClrAssemblyLoader();
}
#endif
/// <summary>
/// Constructor.
/// </summary>
internal TypeLoader(Func<Type, object, bool> isDesiredType)
{
ErrorUtilities.VerifyThrow(isDesiredType != null, "need a type filter");
_isDesiredType = isDesiredType;
}
/// <summary>
/// Given two type names, looks for a partial match between them. A partial match is considered valid only if it occurs on
/// the right side (tail end) of the name strings, and at the start of a class or namespace name.
/// </summary>
/// <remarks>
/// 1) Matches are case-insensitive.
/// 2) .NET conventions regarding namespaces and nested classes are respected, including escaping of reserved characters.
/// </remarks>
/// <example>
/// "Csc" and "csc" ==> exact match
/// "Microsoft.Build.Tasks.Csc" and "Microsoft.Build.Tasks.Csc" ==> exact match
/// "Microsoft.Build.Tasks.Csc" and "Csc" ==> partial match
/// "Microsoft.Build.Tasks.Csc" and "Tasks.Csc" ==> partial match
/// "MyTasks.ATask+NestedTask" and "NestedTask" ==> partial match
/// "MyTasks.ATask\\+NestedTask" and "NestedTask" ==> partial match
/// "MyTasks.CscTask" and "Csc" ==> no match
/// "MyTasks.MyCsc" and "Csc" ==> no match
/// "MyTasks.ATask\.Csc" and "Csc" ==> no match
/// "MyTasks.ATask\\\.Csc" and "Csc" ==> no match
/// </example>
/// <returns>true, if the type names match exactly or partially; false, if there is no match at all</returns>
internal static bool IsPartialTypeNameMatch(string typeName1, string typeName2)
{
bool isPartialMatch = false;
// if the type names are the same length, a partial match is impossible
if (typeName1.Length != typeName2.Length)
{
string longerTypeName;
string shorterTypeName;
// figure out which type name is longer
if (typeName1.Length > typeName2.Length)
{
longerTypeName = typeName1;
shorterTypeName = typeName2;
}
else
{
longerTypeName = typeName2;
shorterTypeName = typeName1;
}
// if the shorter type name matches the end of the longer one
if (longerTypeName.EndsWith(shorterTypeName, StringComparison.OrdinalIgnoreCase))
{
int matchIndex = longerTypeName.Length - shorterTypeName.Length;
// if the matched sub-string looks like the start of a namespace or class name
if ((longerTypeName[matchIndex - 1] == '.') || (longerTypeName[matchIndex - 1] == '+'))
{
int precedingBackslashes = 0;
// confirm there are zero, or an even number of \'s preceding it...
for (int i = matchIndex - 2; i >= 0; i--)
{
if (longerTypeName[i] == '\\')
{
precedingBackslashes++;
}
else
{
break;
}
}
if ((precedingBackslashes % 2) == 0)
{
isPartialMatch = true;
}
}
}
}
else
{
isPartialMatch = (String.Equals(typeName1, typeName2, StringComparison.OrdinalIgnoreCase));
}
return isPartialMatch;
}
/// <summary>
/// Load an assembly given its AssemblyLoadInfo
/// </summary>
/// <param name="assemblyLoadInfo"></param>
/// <returns></returns>
private static Assembly LoadAssembly(AssemblyLoadInfo assemblyLoadInfo)
{
Assembly loadedAssembly = null;
try
{
if (assemblyLoadInfo.AssemblyName != null)
{
#if !FEATURE_ASSEMBLYLOADCONTEXT
loadedAssembly = Assembly.Load(assemblyLoadInfo.AssemblyName);
#else
loadedAssembly = Assembly.Load(new AssemblyName(assemblyLoadInfo.AssemblyName));
#endif
}
else
{
#if !FEATURE_ASSEMBLYLOADCONTEXT
loadedAssembly = Assembly.UnsafeLoadFrom(assemblyLoadInfo.AssemblyFile);
#else
var baseDir = Path.GetDirectoryName(assemblyLoadInfo.AssemblyFile);
s_coreClrAssemblyLoader.AddDependencyLocation(baseDir);
loadedAssembly = s_coreClrAssemblyLoader.LoadFromPath(assemblyLoadInfo.AssemblyFile);
#endif
}
}
catch (ArgumentException e)
{
// Assembly.Load() and Assembly.LoadFrom() will throw an ArgumentException if the assembly name is invalid
// convert to a FileNotFoundException because it's more meaningful
// NOTE: don't use ErrorUtilities.VerifyThrowFileExists() here because that will hit the disk again
throw new FileNotFoundException(null, assemblyLoadInfo.AssemblyLocation, e);
}
return loadedAssembly;
}
/// <summary>
/// Loads the specified type if it exists in the given assembly. If the type name is fully qualified, then a match (if
/// any) is unambiguous; otherwise, if there are multiple types with the same name in different namespaces, the first type
/// found will be returned.
/// </summary>
internal LoadedType Load
(
string typeName,
AssemblyLoadInfo assembly
)
{
return GetLoadedType(s_cacheOfLoadedTypesByFilter, typeName, assembly);
}
/// <summary>
/// Loads the specified type if it exists in the given assembly. If the type name is fully qualified, then a match (if
/// any) is unambiguous; otherwise, if there are multiple types with the same name in different namespaces, the first type
/// found will be returned.
/// </summary>
/// <returns>The loaded type, or null if the type was not found.</returns>
internal LoadedType ReflectionOnlyLoad
(
string typeName,
AssemblyLoadInfo assembly
)
{
return GetLoadedType(s_cacheOfReflectionOnlyLoadedTypesByFilter, typeName, assembly);
}
/// <summary>
/// Loads the specified type if it exists in the given assembly. If the type name is fully qualified, then a match (if
/// any) is unambiguous; otherwise, if there are multiple types with the same name in different namespaces, the first type
/// found will be returned.
/// </summary>
private LoadedType GetLoadedType(ConcurrentDictionary<Func<Type, object, bool>, ConcurrentDictionary<AssemblyLoadInfo, AssemblyInfoToLoadedTypes>> cache, string typeName, AssemblyLoadInfo assembly)
{
// A given type filter have been used on a number of assemblies, Based on the type filter we will get another dictionary which
// will map a specific AssemblyLoadInfo to a AssemblyInfoToLoadedTypes class which knows how to find a typeName in a given assembly.
ConcurrentDictionary<AssemblyLoadInfo, AssemblyInfoToLoadedTypes> loadInfoToType =
cache.GetOrAdd(_isDesiredType, (_) => new ConcurrentDictionary<AssemblyLoadInfo, AssemblyInfoToLoadedTypes>());
// Get an object which is able to take a typename and determine if it is in the assembly pointed to by the AssemblyInfo.
AssemblyInfoToLoadedTypes typeNameToType =
loadInfoToType.GetOrAdd(assembly, (_) => new AssemblyInfoToLoadedTypes(_isDesiredType, _));
return typeNameToType.GetLoadedTypeByTypeName(typeName);
}
/// <summary>
/// Given a type filter and an asssemblyInfo object keep track of what types in a given assembly which match the type filter.
/// Also, use this information to determine if a given TypeName is in the assembly which is pointed to by the AssemblyLoadInfo object.
///
/// This type represents a combination of a type filter and an assemblyInfo object.
/// </summary>
[DebuggerDisplay("Types in {_assemblyLoadInfo} matching {_isDesiredType}")]
private class AssemblyInfoToLoadedTypes
{
/// <summary>
/// Lock to prevent two threads from using this object at the same time.
/// Since we fill up internal structures with what is in the assembly
/// </summary>
private readonly Object _lockObject = new Object();
/// <summary>
/// Type filter to pick the correct types out of an assembly
/// </summary>
private Func<Type, object, bool> _isDesiredType;
/// <summary>
/// Assembly load information so we can load an assembly
/// </summary>
private AssemblyLoadInfo _assemblyLoadInfo;
/// <summary>
/// What is the type for the given type name, this may be null if the typeName does not map to a type.
/// </summary>
private ConcurrentDictionary<string, Type> _typeNameToType;
/// <summary>
/// List of public types in the assembly which match the type filter and their corresponding types
/// </summary>
private Dictionary<string, Type> _publicTypeNameToType;
/// <summary>
/// Have we scanned the public types for this assembly yet.
/// </summary>
private long _haveScannedPublicTypes;
/// <summary>
/// Assembly, if any, that we loaded for this type.
/// We use this information to set the LoadedType.LoadedAssembly so that this object can be used
/// to help created AppDomains to resolve those that it could not load successfully
/// </summary>
private Assembly _loadedAssembly;
/// <summary>
/// Given a type filter, and an assembly to load the type information from determine if a given type name is in the assembly or not.
/// </summary>
internal AssemblyInfoToLoadedTypes(Func<Type, object, bool> typeFilter, AssemblyLoadInfo loadInfo)
{
ErrorUtilities.VerifyThrowArgumentNull(typeFilter, "typefilter");
ErrorUtilities.VerifyThrowArgumentNull(loadInfo, nameof(loadInfo));
_isDesiredType = typeFilter;
_assemblyLoadInfo = loadInfo;
_typeNameToType = new ConcurrentDictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
_publicTypeNameToType = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
}
/// <summary>
/// Determine if a given type name is in the assembly or not. Return null if the type is not in the assembly
/// </summary>
internal LoadedType GetLoadedTypeByTypeName(string typeName)
{
ErrorUtilities.VerifyThrowArgumentNull(typeName, nameof(typeName));
// Only one thread should be doing operations on this instance of the object at a time.
Type type = _typeNameToType.GetOrAdd(typeName, (key) =>
{
if ((_assemblyLoadInfo.AssemblyName != null) && (typeName.Length > 0))
{
try
{
// try to load the type using its assembly qualified name
Type t2 = Type.GetType(typeName + "," + _assemblyLoadInfo.AssemblyName, false /* don't throw on error */, true /* case-insensitive */);
if (t2 != null)
{
return !_isDesiredType(t2, null) ? null : t2;
}
}
catch (ArgumentException)
{
// Type.GetType() will throw this exception if the type name is invalid -- but we have no idea if it's the
// type or the assembly name that's the problem -- so just ignore the exception, because we're going to
// check the existence/validity of the assembly and type respectively, below anyway
}
}
if (Interlocked.Read(ref _haveScannedPublicTypes) == 0)
{
lock (_lockObject)
{
if (Interlocked.Read(ref _haveScannedPublicTypes) == 0)
{
ScanAssemblyForPublicTypes();
Interlocked.Exchange(ref _haveScannedPublicTypes, ~0);
}
}
}
foreach (KeyValuePair<string, Type> desiredTypeInAssembly in _publicTypeNameToType)
{
// if type matches partially on its name
if (typeName.Length == 0 || TypeLoader.IsPartialTypeNameMatch(desiredTypeInAssembly.Key, typeName))
{
return desiredTypeInAssembly.Value;
}
}
return null;
});
return type != null ? new LoadedType(type, _assemblyLoadInfo, _loadedAssembly) : null;
}
/// <summary>
/// Scan the assembly pointed to by the assemblyLoadInfo for public types. We will use these public types to do partial name matching on
/// to find tasks, loggers, and task factories.
/// </summary>
[SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Reflection.Assembly.LoadFrom", Justification = "Necessary in this case.")]
private void ScanAssemblyForPublicTypes()
{
// we need to search the assembly for the type...
_loadedAssembly = LoadAssembly(_assemblyLoadInfo);
// only look at public types
Type[] allPublicTypesInAssembly = _loadedAssembly.GetExportedTypes();
foreach (Type publicType in allPublicTypesInAssembly)
{
if (_isDesiredType(publicType, null))
{
_publicTypeNameToType.Add(publicType.FullName, publicType);
}
}
}
}
}
}