-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
ReflectionModelServices.cs
481 lines (420 loc) · 17.9 KB
/
ReflectionModelServices.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
// 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.Linq;
using System.Reflection;
using Microsoft.Internal;
namespace System.ComponentModel.Composition.ReflectionModel
{
public static class ReflectionModelServices
{
public static Lazy<Type> GetPartType(ComposablePartDefinition partDefinition)
{
Requires.NotNull(partDefinition, nameof(partDefinition));
ReflectionComposablePartDefinition? reflectionPartDefinition = partDefinition as ReflectionComposablePartDefinition;
if (reflectionPartDefinition == null)
{
throw ExceptionBuilder.CreateReflectionModelInvalidPartDefinition(nameof(partDefinition), partDefinition.GetType());
}
return reflectionPartDefinition.GetLazyPartType();
}
public static bool IsDisposalRequired(ComposablePartDefinition partDefinition)
{
Requires.NotNull(partDefinition, nameof(partDefinition));
ReflectionComposablePartDefinition? reflectionPartDefinition = partDefinition as ReflectionComposablePartDefinition;
if (reflectionPartDefinition == null)
{
throw ExceptionBuilder.CreateReflectionModelInvalidPartDefinition(nameof(partDefinition), partDefinition.GetType());
}
return reflectionPartDefinition.IsDisposalRequired;
}
public static LazyMemberInfo GetExportingMember(ExportDefinition exportDefinition)
{
Requires.NotNull(exportDefinition, nameof(exportDefinition));
ReflectionMemberExportDefinition? reflectionExportDefinition = exportDefinition as ReflectionMemberExportDefinition;
if (reflectionExportDefinition == null)
{
throw new ArgumentException(
SR.Format(SR.ReflectionModel_InvalidExportDefinition, exportDefinition.GetType()),
nameof(exportDefinition));
}
return reflectionExportDefinition.ExportingLazyMember;
}
public static LazyMemberInfo GetImportingMember(ImportDefinition importDefinition)
{
Requires.NotNull(importDefinition, nameof(importDefinition));
ReflectionMemberImportDefinition? reflectionMemberImportDefinition = importDefinition as ReflectionMemberImportDefinition;
if (reflectionMemberImportDefinition == null)
{
throw new ArgumentException(
SR.Format(SR.ReflectionModel_InvalidMemberImportDefinition, importDefinition.GetType()),
nameof(importDefinition));
}
return reflectionMemberImportDefinition.ImportingLazyMember;
}
public static Lazy<ParameterInfo> GetImportingParameter(ImportDefinition importDefinition)
{
Requires.NotNull(importDefinition, nameof(importDefinition));
ReflectionParameterImportDefinition? reflectionParameterImportDefinition = importDefinition as ReflectionParameterImportDefinition;
if (reflectionParameterImportDefinition == null)
{
throw new ArgumentException(
SR.Format(SR.ReflectionModel_InvalidParameterImportDefinition, importDefinition.GetType()),
nameof(importDefinition));
}
Debug.Assert(reflectionParameterImportDefinition.ImportingLazyParameter != null);
return reflectionParameterImportDefinition.ImportingLazyParameter;
}
public static bool IsImportingParameter(ImportDefinition importDefinition)
{
Requires.NotNull(importDefinition, nameof(importDefinition));
ReflectionImportDefinition? reflectionImportDefinition = importDefinition as ReflectionImportDefinition;
if (reflectionImportDefinition == null)
{
throw new ArgumentException(
SR.Format(SR.ReflectionModel_InvalidImportDefinition, importDefinition.GetType()),
nameof(importDefinition));
}
return (importDefinition is ReflectionParameterImportDefinition);
}
public static bool IsExportFactoryImportDefinition(ImportDefinition importDefinition)
{
Requires.NotNull(importDefinition, nameof(importDefinition));
return (importDefinition is IPartCreatorImportDefinition);
}
public static ContractBasedImportDefinition GetExportFactoryProductImportDefinition(ImportDefinition importDefinition)
{
Requires.NotNull(importDefinition, nameof(importDefinition));
IPartCreatorImportDefinition? partCreatorImportDefinition = importDefinition as IPartCreatorImportDefinition;
if (partCreatorImportDefinition == null)
{
throw new ArgumentException(
SR.Format(SR.ReflectionModel_InvalidImportDefinition, importDefinition.GetType()),
nameof(importDefinition));
}
return partCreatorImportDefinition.ProductImportDefinition;
}
public static ComposablePartDefinition CreatePartDefinition(
Lazy<Type> partType,
bool isDisposalRequired,
Lazy<IEnumerable<ImportDefinition>>? imports,
Lazy<IEnumerable<ExportDefinition>>? exports,
Lazy<IDictionary<string, object?>>? metadata,
ICompositionElement? origin)
{
Requires.NotNull(partType, nameof(partType));
return new ReflectionComposablePartDefinition(
new ReflectionPartCreationInfo(
partType,
isDisposalRequired,
imports,
exports,
metadata,
origin));
}
public static ExportDefinition CreateExportDefinition(
LazyMemberInfo exportingMember,
string contractName,
Lazy<IDictionary<string, object?>> metadata,
ICompositionElement? origin)
{
Requires.NotNullOrEmpty(contractName, nameof(contractName));
Requires.IsInMembertypeSet(exportingMember.MemberType, nameof(exportingMember), MemberTypes.Field | MemberTypes.Property | MemberTypes.NestedType | MemberTypes.TypeInfo | MemberTypes.Method);
return new ReflectionMemberExportDefinition(
exportingMember,
new LazyExportDefinition(contractName, metadata),
origin);
}
public static ContractBasedImportDefinition CreateImportDefinition(
LazyMemberInfo importingMember,
string contractName,
string? requiredTypeIdentity,
IEnumerable<KeyValuePair<string, Type>>? requiredMetadata,
ImportCardinality cardinality,
bool isRecomposable,
CreationPolicy requiredCreationPolicy,
ICompositionElement? origin)
{
return CreateImportDefinition(importingMember, contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, requiredCreationPolicy, MetadataServices.EmptyMetadata, false, origin);
}
public static ContractBasedImportDefinition CreateImportDefinition(
LazyMemberInfo importingMember,
string contractName,
string? requiredTypeIdentity,
IEnumerable<KeyValuePair<string, Type>>? requiredMetadata,
ImportCardinality cardinality,
bool isRecomposable,
CreationPolicy requiredCreationPolicy,
IDictionary<string, object?> metadata,
bool isExportFactory,
ICompositionElement? origin)
{
return CreateImportDefinition(
importingMember,
contractName,
requiredTypeIdentity,
requiredMetadata,
cardinality,
isRecomposable,
false,
requiredCreationPolicy,
metadata,
isExportFactory,
origin);
}
public static ContractBasedImportDefinition CreateImportDefinition(
LazyMemberInfo importingMember,
string contractName,
string? requiredTypeIdentity,
IEnumerable<KeyValuePair<string, Type>>? requiredMetadata,
ImportCardinality cardinality,
bool isRecomposable,
bool isPreRequisite,
CreationPolicy requiredCreationPolicy,
IDictionary<string, object?> metadata,
bool isExportFactory,
ICompositionElement? origin)
{
Requires.NotNullOrEmpty(contractName, nameof(contractName));
Requires.IsInMembertypeSet(importingMember.MemberType, nameof(importingMember), MemberTypes.Property | MemberTypes.Field);
if (isExportFactory)
{
return new PartCreatorMemberImportDefinition(
importingMember,
origin,
new ContractBasedImportDefinition(
contractName,
requiredTypeIdentity,
requiredMetadata,
cardinality,
isRecomposable,
isPreRequisite,
CreationPolicy.NonShared,
metadata));
}
else
{
return new ReflectionMemberImportDefinition(
importingMember,
contractName,
requiredTypeIdentity,
requiredMetadata,
cardinality,
isRecomposable,
isPreRequisite,
requiredCreationPolicy,
metadata,
origin);
}
}
public static ContractBasedImportDefinition CreateImportDefinition(
Lazy<ParameterInfo> parameter,
string contractName,
string? requiredTypeIdentity,
IEnumerable<KeyValuePair<string, Type>>? requiredMetadata,
ImportCardinality cardinality,
CreationPolicy requiredCreationPolicy,
ICompositionElement? origin)
{
return CreateImportDefinition(parameter, contractName, requiredTypeIdentity, requiredMetadata, cardinality, requiredCreationPolicy, MetadataServices.EmptyMetadata, false, origin);
}
public static ContractBasedImportDefinition CreateImportDefinition(
Lazy<ParameterInfo> parameter,
string contractName,
string? requiredTypeIdentity,
IEnumerable<KeyValuePair<string, Type>>? requiredMetadata,
ImportCardinality cardinality,
CreationPolicy requiredCreationPolicy,
IDictionary<string, object?> metadata,
bool isExportFactory,
ICompositionElement? origin)
{
Requires.NotNull(parameter, nameof(parameter));
Requires.NotNullOrEmpty(contractName, nameof(contractName));
if (isExportFactory)
{
return new PartCreatorParameterImportDefinition(
parameter,
origin,
new ContractBasedImportDefinition(
contractName,
requiredTypeIdentity,
requiredMetadata,
cardinality,
false,
true,
CreationPolicy.NonShared,
metadata));
}
else
{
return new ReflectionParameterImportDefinition(
parameter,
contractName,
requiredTypeIdentity,
requiredMetadata,
cardinality,
requiredCreationPolicy,
metadata,
origin);
}
}
public static bool TryMakeGenericPartDefinition(ComposablePartDefinition partDefinition, IEnumerable<Type> genericParameters, [NotNullWhen(true)] out ComposablePartDefinition? specialization)
{
Requires.NotNull(partDefinition, nameof(partDefinition));
specialization = null;
ReflectionComposablePartDefinition? reflectionPartDefinition = partDefinition as ReflectionComposablePartDefinition;
if (reflectionPartDefinition == null)
{
throw ExceptionBuilder.CreateReflectionModelInvalidPartDefinition(nameof(partDefinition), partDefinition.GetType());
}
return reflectionPartDefinition.TryMakeGenericPartDefinition(genericParameters.ToArray(), out specialization);
}
}
internal sealed class ReflectionPartCreationInfo : IReflectionPartCreationInfo
{
private readonly Lazy<Type> _partType;
private readonly Lazy<IEnumerable<ImportDefinition>>? _imports;
private readonly Lazy<IEnumerable<ExportDefinition>>? _exports;
private readonly Lazy<IDictionary<string, object?>>? _metadata;
private readonly ICompositionElement? _origin;
private ConstructorInfo? _constructor;
private readonly bool _isDisposalRequired;
public ReflectionPartCreationInfo(
Lazy<Type> partType,
bool isDisposalRequired,
Lazy<IEnumerable<ImportDefinition>>? imports,
Lazy<IEnumerable<ExportDefinition>>? exports,
Lazy<IDictionary<string, object?>>? metadata,
ICompositionElement? origin)
{
ArgumentNullException.ThrowIfNull(partType);
_partType = partType;
_isDisposalRequired = isDisposalRequired;
_imports = imports;
_exports = exports;
_metadata = metadata;
_origin = origin;
}
public Type GetPartType()
{
return _partType.GetNotNullValue("type");
}
public Lazy<Type> GetLazyPartType()
{
return _partType;
}
public ConstructorInfo? GetConstructor()
{
if (_constructor == null)
{
ConstructorInfo[]? constructors = null;
constructors = GetImports()
.OfType<ReflectionParameterImportDefinition>()
.Select(parameterImport => parameterImport.ImportingLazyParameter.Value.Member)
.OfType<ConstructorInfo>()
.Distinct()
.ToArray();
if (constructors.Length == 1)
{
_constructor = constructors[0];
}
else if (constructors.Length == 0)
{
_constructor = GetPartType().GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);
}
}
return _constructor;
}
public bool IsDisposalRequired
{
get
{
return _isDisposalRequired;
}
}
public bool IsIdentityComparison
{
get
{
return true;
}
}
public IDictionary<string, object?>? GetMetadata()
{
return _metadata?.Value;
}
public IEnumerable<ExportDefinition> GetExports()
{
if (_exports == null)
{
yield break;
}
IEnumerable<ExportDefinition> exports = _exports.Value;
if (exports == null)
{
yield break;
}
foreach (ExportDefinition export in exports)
{
ReflectionMemberExportDefinition? reflectionExport = export as ReflectionMemberExportDefinition;
if (reflectionExport == null)
{
throw new InvalidOperationException(
SR.Format(SR.ReflectionModel_InvalidExportDefinition, export.GetType()));
}
yield return reflectionExport;
}
}
public IEnumerable<ImportDefinition> GetImports()
{
if (_imports == null)
{
yield break;
}
IEnumerable<ImportDefinition> imports = _imports.Value;
if (imports == null)
{
yield break;
}
foreach (ImportDefinition import in imports)
{
ReflectionImportDefinition? reflectionImport = import as ReflectionImportDefinition;
if (reflectionImport == null)
{
throw new InvalidOperationException(
SR.Format(SR.ReflectionModel_InvalidMemberImportDefinition, import.GetType()));
}
yield return reflectionImport;
}
}
public string DisplayName
{
get { return GetPartType().GetDisplayName(); }
}
public ICompositionElement? Origin
{
get { return _origin; }
}
}
internal sealed class LazyExportDefinition : ExportDefinition
{
private readonly Lazy<IDictionary<string, object?>> _metadata;
public LazyExportDefinition(string contractName, Lazy<IDictionary<string, object?>> metadata)
: base(contractName, (IDictionary<string, object?>?)null)
{
_metadata = metadata;
}
public override IDictionary<string, object?> Metadata
{
get
{
return _metadata.Value ?? MetadataServices.EmptyMetadata;
}
}
}
}