/
AssemblyCatalog.cs
574 lines (538 loc) · 24.5 KB
/
AssemblyCatalog.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.ComponentModel.Composition.Primitives;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Threading;
using Microsoft.Internal;
namespace System.ComponentModel.Composition.Hosting
{
/// <summary>
/// An immutable ComposablePartCatalog created from a managed code assembly.
/// </summary>
/// <remarks>
/// This type is thread safe.
/// </remarks>
[DebuggerTypeProxy(typeof(AssemblyCatalogDebuggerProxy))]
public class AssemblyCatalog : ComposablePartCatalog, ICompositionElement
{
private readonly object _thisLock = new object();
private readonly ICompositionElement _definitionOrigin;
private volatile Assembly _assembly;
private volatile ComposablePartCatalog? _innerCatalog;
private int _isDisposed;
private readonly ReflectionContext? _reflectionContext;
/// <summary>
/// Initializes a new instance of the <see cref="AssemblyCatalog"/> class
/// with the specified code base.
/// </summary>
/// <param name="codeBase">
/// A <see cref="string"/> containing the code base of the assembly containing the
/// attributed <see cref="Type"/> objects to add to the <see cref="AssemblyCatalog"/>.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="codeBase"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="codeBase"/> is a zero-length string, contains only white space,
/// or contains one or more invalid characters. />.
/// </exception>
/// <exception cref="PathTooLongException">
/// The specified path, file name, or both exceed the system-defined maximum length.
/// </exception>
/// <exception cref="System.Security.SecurityException">
/// The caller does not have path discovery permission.
/// </exception>
/// <exception cref="FileNotFoundException">
/// <paramref name="codeBase"/> is not found.
/// </exception>
/// <exception cref="FileLoadException ">
/// <paramref name="codeBase"/> could not be loaded.
/// <para>
/// -or-
/// </para>
/// <paramref name="codeBase"/> specified a directory.
/// </exception>
/// <exception cref="BadImageFormatException">
/// <paramref name="codeBase"/> is not a valid assembly
/// -or-
/// Version 2.0 or later of the common language runtime is currently loaded
/// and <paramref name="codeBase"/> was compiled with a later version.
/// </exception>
/// <remarks>
/// The assembly referenced by <paramref langword="codeBase"/> is loaded into the Load context.
/// </remarks>
public AssemblyCatalog(string codeBase)
{
Requires.NotNullOrEmpty(codeBase, nameof(codeBase));
InitializeAssemblyCatalog(LoadAssembly(codeBase));
_definitionOrigin = this;
}
/// <summary>
/// Initializes a new instance of the <see cref="AssemblyCatalog"/> class
/// with the specified code base.
/// </summary>
/// <param name="codeBase">
/// A <see cref="string"/> containing the code base of the assembly containing the
/// attributed <see cref="Type"/> objects to add to the <see cref="AssemblyCatalog"/>.
/// </param>
/// <param name="reflectionContext">
/// The <see cref="ReflectionContext"/> a context used by the catalog when
/// interpreting the types to inject attributes into the type definition<see cref="AssemblyCatalog"/>.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="codeBase"/> is <see langword="null"/>.
/// <para>
/// -or-
/// </para>
/// <paramref name="reflectionContext"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="codeBase"/> is a zero-length string, contains only white space,
/// or contains one or more invalid characters. />.
/// </exception>
/// <exception cref="PathTooLongException">
/// The specified path, file name, or both exceed the system-defined maximum length.
/// </exception>
/// <exception cref="System.Security.SecurityException">
/// The caller does not have path discovery permission.
/// </exception>
/// <exception cref="FileNotFoundException">
/// <paramref name="codeBase"/> is not found.
/// </exception>
/// <exception cref="FileLoadException ">
/// <paramref name="codeBase"/> could not be loaded.
/// <para>
/// -or-
/// </para>
/// <paramref name="codeBase"/> specified a directory.
/// </exception>
/// <exception cref="BadImageFormatException">
/// <paramref name="codeBase"/> is not a valid assembly
/// -or-
/// Version 2.0 or later of the common language runtime is currently loaded
/// and <paramref name="codeBase"/> was compiled with a later version.
/// </exception>
/// <remarks>
/// The assembly referenced by <paramref langword="codeBase"/> is loaded into the Load context.
/// </remarks>
public AssemblyCatalog(string codeBase, ReflectionContext reflectionContext)
{
Requires.NotNullOrEmpty(codeBase, nameof(codeBase));
Requires.NotNull(reflectionContext, nameof(reflectionContext));
InitializeAssemblyCatalog(LoadAssembly(codeBase));
_reflectionContext = reflectionContext;
_definitionOrigin = this;
}
/// <summary>
/// Initializes a new instance of the <see cref="AssemblyCatalog"/> class
/// with the specified code base.
/// </summary>
/// <param name="codeBase">
/// A <see cref="string"/> containing the code base of the assembly containing the
/// attributed <see cref="Type"/> objects to add to the <see cref="AssemblyCatalog"/>.
/// </param>
/// <param name="definitionOrigin">
/// The <see cref="ICompositionElement"/> CompositionElement used by Diagnostics to identify the source for parts.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="codeBase"/> is <see langword="null"/>.
/// <para>
/// -or-
/// </para>
/// <paramref name="definitionOrigin"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="codeBase"/> is a zero-length string, contains only white space,
/// or contains one or more invalid characters. />.
/// </exception>
/// <exception cref="PathTooLongException">
/// The specified path, file name, or both exceed the system-defined maximum length.
/// </exception>
/// <exception cref="System.Security.SecurityException">
/// The caller does not have path discovery permission.
/// </exception>
/// <exception cref="FileNotFoundException">
/// <paramref name="codeBase"/> is not found.
/// </exception>
/// <exception cref="FileLoadException ">
/// <paramref name="codeBase"/> could not be loaded.
/// <para>
/// -or-
/// </para>
/// <paramref name="codeBase"/> specified a directory.
/// </exception>
/// <exception cref="BadImageFormatException">
/// <paramref name="codeBase"/> is not a valid assembly
/// -or-
/// Version 2.0 or later of the common language runtime is currently loaded
/// and <paramref name="codeBase"/> was compiled with a later version.
/// </exception>
/// <remarks>
/// The assembly referenced by <paramref langword="codeBase"/> is loaded into the Load context.
/// </remarks>
public AssemblyCatalog(string codeBase, ICompositionElement definitionOrigin)
{
Requires.NotNullOrEmpty(codeBase, nameof(codeBase));
Requires.NotNull(definitionOrigin, nameof(definitionOrigin));
InitializeAssemblyCatalog(LoadAssembly(codeBase));
_definitionOrigin = definitionOrigin;
}
/// <summary>
/// Initializes a new instance of the <see cref="AssemblyCatalog"/> class
/// with the specified code base.
/// </summary>
/// <param name="codeBase">
/// A <see cref="string"/> containing the code base of the assembly containing the
/// attributed <see cref="Type"/> objects to add to the <see cref="AssemblyCatalog"/>.
/// </param>
/// <param name="reflectionContext">
/// The <see cref="ReflectionContext"/> a context used by the catalog when
/// interpreting the types to inject attributes into the type definition<see cref="AssemblyCatalog"/>.
/// </param>
/// <param name="definitionOrigin">
/// The <see cref="ICompositionElement"/> CompositionElement used by Diagnostics to identify the source for parts.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="codeBase"/> is <see langword="null"/>.
/// <para>
/// -or-
/// </para>
/// <paramref name="reflectionContext"/> is <see langword="null"/>.
/// <para>
/// -or-
/// </para>
/// <paramref name="definitionOrigin"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="codeBase"/> is a zero-length string, contains only white space,
/// or contains one or more invalid characters. />.
/// </exception>
/// <exception cref="PathTooLongException">
/// The specified path, file name, or both exceed the system-defined maximum length.
/// </exception>
/// <exception cref="System.Security.SecurityException">
/// The caller does not have path discovery permission.
/// </exception>
/// <exception cref="FileNotFoundException">
/// <paramref name="codeBase"/> is not found.
/// </exception>
/// <exception cref="FileLoadException ">
/// <paramref name="codeBase"/> could not be loaded.
/// <para>
/// -or-
/// </para>
/// <paramref name="codeBase"/> specified a directory.
/// </exception>
/// <exception cref="BadImageFormatException">
/// <paramref name="codeBase"/> is not a valid assembly
/// -or-
/// Version 2.0 or later of the common language runtime is currently loaded
/// and <paramref name="codeBase"/> was compiled with a later version.
/// </exception>
/// <remarks>
/// The assembly referenced by <paramref langword="codeBase"/> is loaded into the Load context.
/// </remarks>
public AssemblyCatalog(string codeBase, ReflectionContext reflectionContext, ICompositionElement definitionOrigin)
{
Requires.NotNullOrEmpty(codeBase, nameof(codeBase));
Requires.NotNull(reflectionContext, nameof(reflectionContext));
Requires.NotNull(definitionOrigin, nameof(definitionOrigin));
InitializeAssemblyCatalog(LoadAssembly(codeBase));
_reflectionContext = reflectionContext;
_definitionOrigin = definitionOrigin;
}
/// <summary>
/// Initializes a new instance of the <see cref="AssemblyCatalog"/> class
/// with the specified assembly and reflection context.
/// </summary>
/// <param name="assembly">
/// The <see cref="Assembly"/> containing the attributed <see cref="Type"/> objects to
/// add to the <see cref="AssemblyCatalog"/>.
/// </param>
/// <param name="reflectionContext">
/// The <see cref="ReflectionContext"/> a context used by the catalog when
/// interpreting the types to inject attributes into the type definition.
/// </param>
/// <exception cref="ArgumentException">
/// <paramref name="assembly"/> is <see langword="null"/>.
/// <para>
/// -or-
/// </para>
/// <paramref name="assembly"/> was loaded in the reflection-only context.
/// <para>
/// -or-
/// </para>
/// <paramref name="reflectionContext"/> is <see langword="null"/>.
/// </exception>
public AssemblyCatalog(Assembly assembly, ReflectionContext reflectionContext)
{
Requires.NotNull(assembly, nameof(assembly));
Requires.NotNull(reflectionContext, nameof(reflectionContext));
InitializeAssemblyCatalog(assembly);
_reflectionContext = reflectionContext;
_definitionOrigin = this;
}
/// <summary>
/// Initializes a new instance of the <see cref="AssemblyCatalog"/> class
/// with the specified assembly, reflectionContext and definitionOrigin.
/// </summary>
/// <param name="assembly">
/// The <see cref="Assembly"/> containing the attributed <see cref="Type"/> objects to
/// add to the <see cref="AssemblyCatalog"/>.
/// </param>
/// <param name="reflectionContext">
/// The <see cref="ReflectionContext"/> a context used by the catalog when
/// interpreting the types to inject attributes into the type definition.
/// </param>
/// <param name="definitionOrigin">
/// The <see cref="ICompositionElement"/> CompositionElement used by Diagnostics to identify the source for parts.
/// </param>
/// <exception cref="ArgumentException">
/// <paramref name="assembly"/> is <see langword="null"/>.
/// <para>
/// -or-
/// </para>
/// <paramref name="assembly"/> was loaded in the reflection-only context.
/// <para>
/// -or-
/// </para>
/// <paramref name="reflectionContext"/> is <see langword="null"/>.
/// <para>
/// -or-
/// </para>
/// <paramref name="definitionOrigin"/> is <see langword="null"/>.
/// </exception>
public AssemblyCatalog(Assembly assembly, ReflectionContext reflectionContext, ICompositionElement definitionOrigin)
{
Requires.NotNull(assembly, nameof(assembly));
Requires.NotNull(reflectionContext, nameof(reflectionContext));
Requires.NotNull(definitionOrigin, nameof(definitionOrigin));
InitializeAssemblyCatalog(assembly);
_reflectionContext = reflectionContext;
_definitionOrigin = definitionOrigin;
}
/// <summary>
/// Initializes a new instance of the <see cref="AssemblyCatalog"/> class
/// with the specified assembly.
/// </summary>
/// <param name="assembly">
/// The <see cref="Assembly"/> containing the attributed <see cref="Type"/> objects to
/// add to the <see cref="AssemblyCatalog"/>.
/// </param>
/// <exception cref="ArgumentException">
/// <paramref name="assembly"/> is <see langword="null"/>.
/// <para>
/// -or-
/// </para>
/// <paramref name="assembly"/> was loaded in the reflection-only context.
/// </exception>
public AssemblyCatalog(Assembly assembly)
{
Requires.NotNull(assembly, nameof(assembly));
InitializeAssemblyCatalog(assembly);
_definitionOrigin = this;
}
/// <summary>
/// Initializes a new instance of the <see cref="AssemblyCatalog"/> class
/// with the specified assembly.
/// </summary>
/// <param name="assembly">
/// The <see cref="Assembly"/> containing the attributed <see cref="Type"/> objects to
/// add to the <see cref="AssemblyCatalog"/>.
/// </param>
/// <param name="definitionOrigin">
/// The <see cref="ICompositionElement"/> CompositionElement used by Diagnostics to identify the source for parts.
/// </param>
/// <exception cref="ArgumentException">
/// <paramref name="assembly"/> is <see langword="null"/>.
/// <para>
/// -or-
/// </para>
/// <paramref name="assembly"/> was loaded in the reflection-only context.
/// <para>
/// -or-
/// </para>
/// <paramref name="definitionOrigin"/> is <see langword="null"/>.
/// </exception>
public AssemblyCatalog(Assembly assembly, ICompositionElement definitionOrigin)
{
Requires.NotNull(assembly, nameof(assembly));
Requires.NotNull(definitionOrigin, nameof(definitionOrigin));
InitializeAssemblyCatalog(assembly);
_definitionOrigin = definitionOrigin;
}
[MemberNotNull(nameof(_assembly))]
private void InitializeAssemblyCatalog(Assembly assembly)
{
if (assembly.ReflectionOnly)
{
throw new ArgumentException(SR.Format(SR.Argument_AssemblyReflectionOnly, nameof(assembly)), nameof(assembly));
}
_assembly = assembly;
}
/// <summary>
/// Returns the export definitions that match the constraint defined by the specified definition.
/// </summary>
/// <param name="definition">
/// The <see cref="ImportDefinition"/> that defines the conditions of the
/// <see cref="ExportDefinition"/> objects to return.
/// </param>
/// <returns>
/// An <see cref="IEnumerable{T}"/> of <see cref="Tuple{T1, T2}"/> containing the
/// <see cref="ExportDefinition"/> objects and their associated
/// <see cref="ComposablePartDefinition"/> for objects that match the constraint defined
/// by <paramref name="definition"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="definition"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The <see cref="ComposablePartCatalog"/> has been disposed of.
/// </exception>
/// <remarks>
/// <note type="inheritinfo">
/// Overriders of this property should never return <see langword="null"/>, if no
/// <see cref="ExportDefinition"/> match the conditions defined by
/// <paramref name="definition"/>, return an empty <see cref="IEnumerable{T}"/>.
/// </note>
/// </remarks>
public override IEnumerable<Tuple<ComposablePartDefinition, ExportDefinition>> GetExports(ImportDefinition definition)
{
return InnerCatalog.GetExports(definition);
}
private ComposablePartCatalog InnerCatalog
{
get
{
ThrowIfDisposed();
if (_innerCatalog == null)
{
var catalogReflectionContextAttribute = _assembly.GetFirstAttribute<CatalogReflectionContextAttribute>();
var assembly = (catalogReflectionContextAttribute != null)
? catalogReflectionContextAttribute.CreateReflectionContext().MapAssembly(_assembly)
: _assembly;
lock (_thisLock)
{
if (_innerCatalog == null)
{
var catalog = (_reflectionContext != null)
? new TypeCatalog(assembly.GetTypes(), _reflectionContext, _definitionOrigin)
: new TypeCatalog(assembly.GetTypes(), _definitionOrigin);
Thread.MemoryBarrier();
_innerCatalog = catalog;
}
}
}
return _innerCatalog;
}
}
/// <summary>
/// Gets the assembly containing the attributed types contained within the assembly
/// catalog.
/// </summary>
/// <value>
/// The <see cref="Assembly"/> containing the attributed <see cref="Type"/> objects
/// contained within the <see cref="AssemblyCatalog"/>.
/// </value>
public Assembly Assembly
{
get
{
Debug.Assert(_assembly != null);
return _assembly;
}
}
/// <summary>
/// Gets the display name of the assembly catalog.
/// </summary>
/// <value>
/// A <see cref="string"/> containing a human-readable display name of the <see cref="AssemblyCatalog"/>.
/// </value>
string ICompositionElement.DisplayName
{
get { return GetDisplayName(); }
}
/// <summary>
/// Gets the composition element from which the assembly catalog originated.
/// </summary>
/// <value>
/// This property always returns <see langword="null"/>.
/// </value>
ICompositionElement? ICompositionElement.Origin
{
get { return null; }
}
/// <summary>
/// Returns a string representation of the assembly catalog.
/// </summary>
/// <returns>
/// A <see cref="string"/> containing the string representation of the <see cref="AssemblyCatalog"/>.
/// </returns>
public override string ToString()
{
return GetDisplayName();
}
protected override void Dispose(bool disposing)
{
try
{
if (Interlocked.CompareExchange(ref _isDisposed, 1, 0) == 0)
{
if (disposing)
{
_innerCatalog?.Dispose();
}
}
}
finally
{
base.Dispose(disposing);
}
}
public override IEnumerator<ComposablePartDefinition> GetEnumerator()
{
return InnerCatalog.GetEnumerator();
}
private void ThrowIfDisposed()
{
if (_isDisposed == 1)
{
throw ExceptionBuilder.CreateObjectDisposed(this);
}
}
private string GetDisplayName() =>
$"{GetType().Name} (Assembly=\"{Assembly.FullName}\")"; // NOLOC
[UnconditionalSuppressMessage("SingleFile", "IL3000: Avoid accessing Assembly file path when publishing as a single file",
Justification = "Setting a CodeBase is single file compatible")]
private static Assembly LoadAssembly(string codeBase)
{
Requires.NotNullOrEmpty(codeBase, nameof(codeBase));
AssemblyName assemblyName;
try
{
assemblyName = AssemblyName.GetAssemblyName(codeBase);
}
catch (ArgumentException)
{
assemblyName = new AssemblyName();
#pragma warning disable SYSLIB0044 // AssemblyName.CodeBase and AssemblyName.EscapedCodeBase are obsolete. Using them for loading an assembly is not supported.
assemblyName.CodeBase = codeBase;
#pragma warning restore SYSLIB0044
}
try
{
return Assembly.Load(assemblyName);
}
//fallback attempt issue https://github.com/dotnet/runtime/issues/25177
catch (FileNotFoundException)
{
return Assembly.LoadFrom(codeBase);
}
}
}
}