/
CompositionScopeDefinition.cs
257 lines (230 loc) · 9.99 KB
/
CompositionScopeDefinition.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
// 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.Linq;
using System.Threading;
using Microsoft.Internal;
namespace System.ComponentModel.Composition.Hosting
{
[DebuggerTypeProxy(typeof(CompositionScopeDefinitionDebuggerProxy))]
public class CompositionScopeDefinition : ComposablePartCatalog, INotifyComposablePartCatalogChanged
{
private ComposablePartCatalog? _catalog;
private IEnumerable<ExportDefinition>? _publicSurface;
private IEnumerable<CompositionScopeDefinition> _children = Enumerable.Empty<CompositionScopeDefinition>();
private volatile int _isDisposed;
/// <summary>
/// Initializes a new instance of the <see cref="CompositionScopeDefinition"/> class.
/// </summary>
protected CompositionScopeDefinition() { }
/// <summary>
/// Initializes a new instance of the <see cref="CompositionScopeDefinition"/> class.
/// </summary>
/// <param name="catalog">The catalog.</param>
/// <param name="children">The children.</param>
public CompositionScopeDefinition(ComposablePartCatalog catalog, IEnumerable<CompositionScopeDefinition> children)
{
Requires.NotNull(catalog, nameof(catalog));
Requires.NullOrNotNullElements(children, nameof(children));
InitializeCompositionScopeDefinition(catalog, children, null);
}
/// <summary>
/// Initializes a new instance of the <see cref="CompositionScopeDefinition"/> class.
/// </summary>
/// <param name="catalog">The catalog.</param>
/// <param name="children">The children.</param>
/// <param name="publicSurface">The exports that can be used to create new scopes.</param>
public CompositionScopeDefinition(ComposablePartCatalog catalog, IEnumerable<CompositionScopeDefinition> children, IEnumerable<ExportDefinition> publicSurface)
{
Requires.NotNull(catalog, nameof(catalog));
Requires.NullOrNotNullElements(children, nameof(children));
Requires.NullOrNotNullElements(publicSurface, nameof(publicSurface));
InitializeCompositionScopeDefinition(catalog, children, publicSurface);
}
/// <summary>
/// Initializes a new instance of the <see cref="CompositionScopeDefinition"/> class.
/// </summary>
/// <param name="catalog">The catalog.</param>
/// <param name="children">The children.</param>
/// <param name="publicSurface">The exports that can be used to create new scopes.</param>
private void InitializeCompositionScopeDefinition(ComposablePartCatalog catalog, IEnumerable<CompositionScopeDefinition>? children, IEnumerable<ExportDefinition>? publicSurface)
{
_catalog = catalog;
if (children != null)
{
_children = children.ToArray();
}
if (publicSurface != null)
{
_publicSurface = publicSurface;
}
if (_catalog is INotifyComposablePartCatalogChanged notifyCatalog)
{
notifyCatalog.Changed += OnChangedInternal;
notifyCatalog.Changing += OnChangingInternal;
}
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
try
{
if (disposing)
{
if (Interlocked.CompareExchange(ref _isDisposed, 1, 0) == 0)
{
if (_catalog is INotifyComposablePartCatalogChanged notifyCatalog)
{
notifyCatalog.Changed -= OnChangedInternal;
notifyCatalog.Changing -= OnChangingInternal;
}
}
}
}
finally
{
base.Dispose(disposing);
}
}
/// <summary>
/// Gets the children.
/// </summary>
/// <value>The children.</value>
public virtual IEnumerable<CompositionScopeDefinition> Children
{
get
{
ThrowIfDisposed();
return _children;
}
}
/// <summary>
/// Gets the export definitions that describe the exports surfaced by the CompositionScopedefinition.
/// </summary>
/// <value>
/// An <see cref="IEnumerable{T}"/> of <see cref="ExportDefinition"/> objects describing
/// the exports surfaced by the <see cref="CompositionScopeDefinition"/>.
/// </value>
/// <remarks>
/// <note type="inheritinfo">
/// Overriders of this property must not return <see langword="null"/>.
/// </note>
/// </remarks>
public virtual IEnumerable<ExportDefinition> PublicSurface
{
get
{
ThrowIfDisposed();
if (_publicSurface == null)
{
return this.SelectMany((p) => p.ExportDefinitions);
}
return _publicSurface;
}
}
/// <summary>
/// Gets an Enumerator for the ComposablePartDefinitions
/// </summary>
/// <value>The children.</value>
public override IEnumerator<ComposablePartDefinition> GetEnumerator()
{
Debug.Assert(_catalog != null);
return _catalog.GetEnumerator();
}
/// <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)
{
ThrowIfDisposed();
Debug.Assert(_catalog != null);
return _catalog.GetExports(definition);
}
internal IEnumerable<Tuple<ComposablePartDefinition, ExportDefinition>> GetExportsFromPublicSurface(ImportDefinition definition)
{
ArgumentNullException.ThrowIfNull(definition);
var exports = new List<Tuple<ComposablePartDefinition, ExportDefinition>>();
foreach (var exportDefinition in PublicSurface)
{
if (definition.IsConstraintSatisfiedBy(exportDefinition))
{
foreach (var export in GetExports(definition))
{
if (export.Item2 == exportDefinition)
{
exports.Add(export);
break;
}
}
}
}
return exports;
}
/// <summary>
/// Notify when the contents of the Catalog has changed.
/// </summary>
public event EventHandler<ComposablePartCatalogChangeEventArgs>? Changed;
/// <summary>
/// Notify when the contents of the Catalog is changing.
/// </summary>
public event EventHandler<ComposablePartCatalogChangeEventArgs>? Changing;
/// <summary>
/// Raises the <see cref="Changed"/> event.
/// </summary>
/// <param name="e">The <see cref="System.ComponentModel.Composition.Hosting.ComposablePartCatalogChangeEventArgs"/> instance containing the event data.</param>
protected virtual void OnChanged(ComposablePartCatalogChangeEventArgs e)
{
Changed?.Invoke(this, e);
}
/// <summary>
/// Raises the <see cref="Changing"/> event.
/// </summary>
/// <param name="e">The <see cref="System.ComponentModel.Composition.Hosting.ComposablePartCatalogChangeEventArgs"/> instance containing the event data.</param>
protected virtual void OnChanging(ComposablePartCatalogChangeEventArgs e)
{
Changing?.Invoke(this, e);
}
private void OnChangedInternal(object? sender, ComposablePartCatalogChangeEventArgs e)
{
OnChanged(e);
}
private void OnChangingInternal(object? sender, ComposablePartCatalogChangeEventArgs e)
{
OnChanging(e);
}
[DebuggerStepThrough]
private void ThrowIfDisposed()
{
if (_isDisposed == 1)
{
throw ExceptionBuilder.CreateObjectDisposed(this);
}
}
}
}