/
ComposablePartDefinition.cs
344 lines (301 loc) · 16.2 KB
/
ComposablePartDefinition.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
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.VisualStudio.Composition
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Composition.Reflection;
[DebuggerDisplay("{" + nameof(Type) + ".Name}")]
public class ComposablePartDefinition : IEquatable<ComposablePartDefinition>
{
/// <summary>
/// Initializes a new instance of the <see cref="ComposablePartDefinition"/> class.
/// </summary>
/// <param name="partType">Type of the part.</param>
/// <param name="metadata">The metadata discovered on the part.</param>
/// <param name="exportedTypes">The exported types.</param>
/// <param name="exportingMembers">The exporting members.</param>
/// <param name="importingMembers">The importing members.</param>
/// <param name="sharingBoundary">The sharing boundary that this part is shared within.</param>
/// <param name="onImportsSatisfied">The method to invoke after satisfying imports, if any.</param>
/// <param name="importingConstructorRef">The constructor to invoke to construct the part.</param>
/// <param name="importingConstructorImports">The importing arguments taken by the importing constructor. <c>null</c> if the part cannot be instantiated.</param>
/// <param name="partCreationPolicy">The creation policy for this part.</param>
/// <param name="isSharingBoundaryInferred">A value indicating whether the part does not have an explicit sharing boundary, and therefore can obtain its sharing boundary based on its imports.</param>
public ComposablePartDefinition(TypeRef partType, IReadOnlyDictionary<string, object> metadata, IReadOnlyCollection<ExportDefinition> exportedTypes, IReadOnlyDictionary<MemberRef, IReadOnlyCollection<ExportDefinition>> exportingMembers, IEnumerable<ImportDefinitionBinding> importingMembers, string sharingBoundary, MethodRef onImportsSatisfied, MethodRef importingConstructorRef, IReadOnlyList<ImportDefinitionBinding> importingConstructorImports, CreationPolicy partCreationPolicy, bool isSharingBoundaryInferred = false)
{
Requires.NotNull(partType, nameof(partType));
Requires.NotNull(metadata, nameof(metadata));
Requires.NotNull(exportedTypes, nameof(exportedTypes));
Requires.NotNull(exportingMembers, nameof(exportingMembers));
Requires.NotNull(importingMembers, nameof(importingMembers));
this.TypeRef = partType;
this.Metadata = metadata;
this.ExportedTypes = exportedTypes;
this.ExportingMembers = exportingMembers;
this.ImportingMembers = ImmutableHashSet.CreateRange(importingMembers);
this.SharingBoundary = sharingBoundary;
this.OnImportsSatisfiedRef = onImportsSatisfied;
this.ImportingConstructorOrFactoryRef = importingConstructorRef;
this.ImportingConstructorImports = importingConstructorImports;
this.CreationPolicy = partCreationPolicy;
this.IsSharingBoundaryInferred = isSharingBoundaryInferred;
this.ExtraInputAssemblies = Enumerable.Empty<AssemblyName>();
}
/// <summary>
/// Initializes a new instance of the <see cref="ComposablePartDefinition"/> class.
/// </summary>
/// <param name="partType">Type of the part.</param>
/// <param name="metadata">The metadata discovered on the part.</param>
/// <param name="exportedTypes">The exported types.</param>
/// <param name="exportingMembers">The exporting members.</param>
/// <param name="importingMembers">The importing members.</param>
/// <param name="sharingBoundary">The sharing boundary that this part is shared within.</param>
/// <param name="onImportsSatisfied">The method to invoke after satisfying imports, if any.</param>
/// <param name="importingConstructorRef">The constructor to invoke to construct the part.</param>
/// <param name="importingConstructorImports">The importing arguments taken by the importing constructor. <c>null</c> if the part cannot be instantiated.</param>
/// <param name="partCreationPolicy">The creation policy for this part.</param>
/// <param name="extraInputAssemblies">A sequence of extra assemblies to be added to the set for <see cref="GetInputAssemblies(ISet{AssemblyName})"/></param>
/// <param name="isSharingBoundaryInferred">A value indicating whether the part does not have an explicit sharing boundary, and therefore can obtain its sharing boundary based on its imports.</param>
public ComposablePartDefinition(TypeRef partType, IReadOnlyDictionary<string, object> metadata, IReadOnlyCollection<ExportDefinition> exportedTypes, IReadOnlyDictionary<MemberRef, IReadOnlyCollection<ExportDefinition>> exportingMembers, IEnumerable<ImportDefinitionBinding> importingMembers, string sharingBoundary, MethodRef onImportsSatisfied, MethodRef importingConstructorRef, IReadOnlyList<ImportDefinitionBinding> importingConstructorImports, CreationPolicy partCreationPolicy, IEnumerable<AssemblyName> extraInputAssemblies, bool isSharingBoundaryInferred = false)
: this(partType, metadata, exportedTypes, exportingMembers, importingMembers, sharingBoundary, onImportsSatisfied, importingConstructorRef, importingConstructorImports, partCreationPolicy, isSharingBoundaryInferred)
{
Requires.NotNull(extraInputAssemblies, nameof(extraInputAssemblies));
this.ExtraInputAssemblies = extraInputAssemblies;
}
public Type Type
{
get { return this.TypeRef.Resolve(); }
}
public TypeRef TypeRef { get; private set; }
public string Id
{
get { return this.Type.FullName.Replace('`', '_').Replace('.', '_').Replace('+', '_'); }
}
public string SharingBoundary { get; private set; }
/// <summary>
/// Gets a value indicating whether the sharing boundary must be inferred from what is imported.
/// </summary>
/// <remarks>
/// This is <c>true</c> when the part was discovered by MEFv1 attributes, since these attributes do not have
/// a way to convey a sharing boundary.
/// This is <c>false</c> when the part is discovered by MEFv2 attributes, which have a SharedAttribute(string) that they can use
/// to specify the value.
/// When this is <c>true</c>, the <see cref="SharingBoundary"/> property is set to <see cref="string.Empty"/>.
/// </remarks>
public bool IsSharingBoundaryInferred { get; private set; }
public CreationPolicy CreationPolicy { get; private set; }
public bool IsShared
{
get { return this.SharingBoundary != null; }
}
/// <summary>
/// Gets the metadata for this part.
/// </summary>
/// <remarks>
/// This metadata has no effect on composition, but may be useful if the host
/// wishes to filter a catalog based on part metadata prior to creating a composition.
/// </remarks>
public IReadOnlyDictionary<string, object> Metadata { get; private set; }
public MethodInfo OnImportsSatisfied
{
get { return (MethodInfo)this.OnImportsSatisfiedRef?.MethodBase; }
}
public MethodRef OnImportsSatisfiedRef { get; private set; }
/// <summary>
/// Gets the types exported on the part itself.
/// </summary>
public IReadOnlyCollection<ExportDefinition> ExportedTypes { get; private set; }
/// <summary>
/// Gets the exports found on members of the part (exporting properties, fields, methods.)
/// </summary>
public IReadOnlyDictionary<MemberRef, IReadOnlyCollection<ExportDefinition>> ExportingMembers { get; private set; }
/// <summary>
/// Gets a sequence of all exports found on this part (both the type directly and its members).
/// </summary>
public IEnumerable<KeyValuePair<MemberRef, ExportDefinition>> ExportDefinitions
{
get
{
foreach (var export in this.ExportedTypes)
{
yield return new KeyValuePair<MemberRef, ExportDefinition>(default(MemberRef), export);
}
foreach (var member in this.ExportingMembers)
{
foreach (var export in member.Value)
{
yield return new KeyValuePair<MemberRef, ExportDefinition>(member.Key, export);
}
}
}
}
/// <summary>
/// Gets the sequence of extra input assemblies that will be added to the
/// input assemblies for this <see cref="ComposablePartDefinition"/>.
/// </summary>
public IEnumerable<AssemblyName> ExtraInputAssemblies { get; }
public ImmutableHashSet<ImportDefinitionBinding> ImportingMembers { get; private set; }
/// <summary>
/// Gets the list of parameters on the importing constructor,
/// or <c>null</c> if the part cannot be instantiated.
/// </summary>
public IReadOnlyList<ImportDefinitionBinding> ImportingConstructorImports { get; private set; }
public bool IsInstantiable
{
get { return this.ImportingConstructorImports != null; }
}
public MethodRef ImportingConstructorOrFactoryRef { get; }
public MethodBase ImportingConstructorOrFactory => this.ImportingConstructorOrFactoryRef?.MethodBase;
/// <summary>
/// Gets a sequence of all imports found on this part (both members and importing constructor).
/// </summary>
public IEnumerable<ImportDefinitionBinding> Imports
{
get
{
IEnumerable<ImportDefinitionBinding> result = this.ImportingMembers;
if (this.ImportingConstructorImports != null)
{
result = result.Concat(this.ImportingConstructorImports);
}
return result;
}
}
public override int GetHashCode()
{
return this.TypeRef.GetHashCode();
}
public override bool Equals(object obj)
{
return this.Equals(obj as ComposablePartDefinition);
}
public bool Equals(ComposablePartDefinition other)
{
if (other == null)
{
return false;
}
if (object.ReferenceEquals(this, other))
{
return true;
}
bool result = EqualityComparer<TypeRef>.Default.Equals(this.TypeRef, other.TypeRef)
&& ByValueEquality.Metadata.Equals(this.Metadata, other.Metadata)
&& this.SharingBoundary == other.SharingBoundary
&& this.IsSharingBoundaryInferred == other.IsSharingBoundaryInferred
&& this.CreationPolicy == other.CreationPolicy
&& EqualityComparer<MethodRef>.Default.Equals(this.OnImportsSatisfiedRef, other.OnImportsSatisfiedRef)
&& ByValueEquality.EquivalentIgnoreOrder<ExportDefinition>().Equals(this.ExportedTypes, other.ExportedTypes)
&& ByValueEquality.Dictionary<MemberRef, IReadOnlyCollection<ExportDefinition>>(ByValueEquality.EquivalentIgnoreOrder<ExportDefinition>()).Equals(this.ExportingMembers, other.ExportingMembers)
&& EqualityComparer<MethodRef>.Default.Equals(this.ImportingConstructorOrFactoryRef, other.ImportingConstructorOrFactoryRef)
&& this.ImportingMembers.SetEquals(other.ImportingMembers)
&& ((this.ImportingConstructorImports == null && other.ImportingConstructorImports == null) || (this.ImportingConstructorImports != null && other.ImportingConstructorImports != null && this.ImportingConstructorImports.SequenceEqual(other.ImportingConstructorImports)));
return result;
}
public void ToString(TextWriter writer)
{
var indentingWriter = IndentingTextWriter.Get(writer);
indentingWriter.WriteLine("Type: {0}", this.Type.FullName);
if (this.Metadata.Count > 0)
{
indentingWriter.WriteLine("Part metadata:");
using (indentingWriter.Indent())
{
foreach (var item in this.Metadata)
{
indentingWriter.WriteLine("{0} = {1}", item.Key, item.Value);
}
}
}
indentingWriter.WriteLine("SharingBoundary: {0}", this.SharingBoundary.SpecifyIfNull());
indentingWriter.WriteLine("IsSharingBoundaryInferred: {0}", this.IsSharingBoundaryInferred);
indentingWriter.WriteLine("CreationPolicy: {0}", this.CreationPolicy);
indentingWriter.WriteLine("OnImportsSatisfied: {0}", this.OnImportsSatisfied.SpecifyIfNull());
indentingWriter.WriteLine("ExportedTypes:");
using (indentingWriter.Indent())
{
foreach (var item in this.ExportedTypes.OrderBy(et => et.ContractName))
{
indentingWriter.WriteLine("ExportDefinition");
using (indentingWriter.Indent())
{
item.ToString(indentingWriter);
}
}
}
indentingWriter.WriteLine("ExportingMembers:");
using (indentingWriter.Indent())
{
foreach (var exportingMember in this.ExportingMembers)
{
indentingWriter.WriteLine(exportingMember.Key.MemberInfo.Name);
using (indentingWriter.Indent())
{
foreach (var export in exportingMember.Value)
{
export.ToString(indentingWriter);
}
}
}
}
indentingWriter.WriteLine("ImportingMembers:");
using (indentingWriter.Indent())
{
foreach (var importingMember in this.ImportingMembers)
{
importingMember.ToString(indentingWriter);
}
}
if (this.ImportingConstructorImports == null)
{
indentingWriter.WriteLine("ImportingConstructor: <null>");
}
else
{
indentingWriter.WriteLine("ImportingConstructor:");
using (indentingWriter.Indent())
{
foreach (var import in this.ImportingConstructorImports)
{
import.ToString(indentingWriter);
}
}
}
}
internal void GetInputAssemblies(ISet<AssemblyName> assemblies)
{
Requires.NotNull(assemblies, nameof(assemblies));
foreach (var inputAssembly in this.ExtraInputAssemblies)
{
assemblies.Add(inputAssembly);
}
this.TypeRef.GetInputAssemblies(assemblies);
ReflectionHelpers.GetInputAssembliesFromMetadata(assemblies, this.Metadata);
foreach (var export in this.ExportedTypes)
{
export.GetInputAssemblies(assemblies);
}
foreach (var exportingMember in this.ExportingMembers)
{
exportingMember.Key.GetInputAssemblies(assemblies);
foreach (var export in exportingMember.Value)
{
export.GetInputAssemblies(assemblies);
}
}
foreach (var import in this.Imports)
{
import.GetInputAssemblies(assemblies);
}
this.OnImportsSatisfiedRef?.GetInputAssemblies(assemblies);
this.ImportingConstructorOrFactoryRef?.GetInputAssemblies(assemblies);
}
}
}