/
ComposablePartCatalog.cs
173 lines (158 loc) · 7.2 KB
/
ComposablePartCatalog.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
// 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.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using Microsoft.Internal;
using Microsoft.Internal.Collections;
namespace System.ComponentModel.Composition.Primitives
{
/// <summary>
/// Defines the <see langword="abstract"/> base class for composable part catalogs, which produce
/// and return <see cref="ComposablePartDefinition"/> objects.
/// </summary>
/// <remarks>
/// This type is thread safe.
/// </remarks>
[DebuggerTypeProxy(typeof(ComposablePartCatalogDebuggerProxy))]
public abstract class ComposablePartCatalog : IEnumerable<ComposablePartDefinition>, IDisposable
{
private bool _isDisposed;
private volatile IQueryable<ComposablePartDefinition>? _queryableParts;
internal static readonly List<Tuple<ComposablePartDefinition, ExportDefinition>> _EmptyExportsList = new List<Tuple<ComposablePartDefinition, ExportDefinition>>();
/// <summary>
/// Initializes a new instance of the <see cref="ComposablePartCatalog"/> class.
/// </summary>
protected ComposablePartCatalog()
{
}
/// <summary>
/// Gets the part definitions of the catalog.
/// </summary>
/// <value>
/// A <see cref="IQueryable{T}"/> of <see cref="ComposablePartDefinition"/> objects of the
/// <see cref="ComposablePartCatalog"/>.
/// </value>
/// <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"/>.
/// </note>
/// </remarks>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
public virtual IQueryable<ComposablePartDefinition> Parts
{
get
{
ThrowIfDisposed();
if (_queryableParts == null)
{
// Guarantee one time only set _queryableParts
var p = this.AsQueryable();
Interlocked.CompareExchange(ref _queryableParts, p, null);
if (_queryableParts == null)
{
throw new Exception(SR.Diagnostic_InternalExceptionMessage);
}
}
return _queryableParts;
}
}
/// <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 virtual IEnumerable<Tuple<ComposablePartDefinition, ExportDefinition>> GetExports(ImportDefinition definition)
{
ThrowIfDisposed();
Requires.NotNull(definition, nameof(definition));
List<Tuple<ComposablePartDefinition, ExportDefinition>>? exports = null;
var candidateParts = GetCandidateParts(definition);
if (candidateParts != null)
{
foreach (var part in candidateParts)
{
if (part.TryGetExports(definition, out Tuple<ComposablePartDefinition, ExportDefinition>? singleMatch, out IEnumerable<Tuple<ComposablePartDefinition, ExportDefinition>>? multipleMatches))
{
exports = exports.FastAppendToListAllowNulls(singleMatch, multipleMatches);
}
}
}
Debug.Assert(exports != null || _EmptyExportsList != null);
return exports ?? _EmptyExportsList;
}
internal virtual IEnumerable<ComposablePartDefinition>? GetCandidateParts(ImportDefinition definition)
{
return this;
}
/// <summary>
/// Releases the unmanaged and managed resources used by the <see cref="ComposablePartCatalog"/>.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
_isDisposed = true;
}
[DebuggerStepThrough]
private void ThrowIfDisposed()
{
if (_isDisposed)
{
throw ExceptionBuilder.CreateObjectDisposed(this);
}
}
//
// If neither Parts nor GetEnumerator() is overridden then return an empty list
// If GetEnumerator is overridden this code should not be invoked: ReferenceAssemblies mark it as Abstract or Not present
// We verify whether Parts is overridden by seeing if the object returns matched the one cached for this instance
// Note: a query object is only cached if Parts is invoked on a catalog which did not implement it
// Because reference assemblies do not expose Parts and we no longer use it, it should not get invoked by 3rd parties
// Because the reference assemblies mark GetEnumerator as Abstract 3rd party code should not lack an implementation
// That implementation should not try to call this implementation
// Our code doies delegate to Parts in the DebuggerProxies of course.
//
public virtual IEnumerator<ComposablePartDefinition> GetEnumerator()
{
var parts = Parts;
if (object.ReferenceEquals(parts, _queryableParts))
{
return Enumerable.Empty<ComposablePartDefinition>().GetEnumerator();
}
return parts.GetEnumerator();
}
Collections.IEnumerator Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}