/
ApplicationCatalog.cs
209 lines (184 loc) · 7.94 KB
/
ApplicationCatalog.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
// 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 Microsoft.Internal;
namespace System.ComponentModel.Composition.Hosting
{
public partial class ApplicationCatalog : ComposablePartCatalog, ICompositionElement
{
private bool _isDisposed;
private volatile AggregateCatalog? _innerCatalog;
private readonly object _thisLock = new object();
private readonly ICompositionElement? _definitionOrigin;
private readonly ReflectionContext? _reflectionContext;
public ApplicationCatalog() { }
public ApplicationCatalog(ICompositionElement definitionOrigin)
{
Requires.NotNull(definitionOrigin, nameof(definitionOrigin));
_definitionOrigin = definitionOrigin;
}
public ApplicationCatalog(ReflectionContext reflectionContext)
{
Requires.NotNull(reflectionContext, nameof(reflectionContext));
_reflectionContext = reflectionContext;
}
public ApplicationCatalog(ReflectionContext reflectionContext, ICompositionElement definitionOrigin)
{
Requires.NotNull(reflectionContext, nameof(reflectionContext));
Requires.NotNull(definitionOrigin, nameof(definitionOrigin));
_reflectionContext = reflectionContext;
_definitionOrigin = definitionOrigin;
}
internal ComposablePartCatalog CreateCatalog(string location, string pattern)
{
if (_reflectionContext != null)
{
return (_definitionOrigin != null)
? new DirectoryCatalog(location, pattern, _reflectionContext, _definitionOrigin)
: new DirectoryCatalog(location, pattern, _reflectionContext);
}
return (_definitionOrigin != null)
? new DirectoryCatalog(location, pattern, _definitionOrigin)
: new DirectoryCatalog(location, pattern);
}
// Note:
// Creating a catalog does not cause change notifications to propagate, For some reason the DeploymentCatalog did, but that is a bug.
// InnerCatalog is delay evaluated, from data supplied at construction time and so does not propagate change notifications
private AggregateCatalog InnerCatalog
{
get
{
if (_innerCatalog == null)
{
lock (_thisLock)
{
if (_innerCatalog == null)
{
var location = AppDomain.CurrentDomain.BaseDirectory;
if (location == null)
{
throw new Exception(SR.Diagnostic_InternalExceptionMessage);
}
var catalogs = new List<ComposablePartCatalog>();
catalogs.Add(CreateCatalog(location, "*.exe"));
catalogs.Add(CreateCatalog(location, "*.dll"));
string? relativeSearchPath = AppDomain.CurrentDomain.RelativeSearchPath;
if (!string.IsNullOrEmpty(relativeSearchPath))
{
string[] probingPaths = relativeSearchPath.Split(';', StringSplitOptions.RemoveEmptyEntries);
foreach (var probingPath in probingPaths)
{
var path = Path.Combine(location, probingPath);
if (Directory.Exists(path))
{
catalogs.Add(CreateCatalog(path, "*.dll"));
}
}
}
var innerCatalog = new AggregateCatalog(catalogs);
_innerCatalog = innerCatalog;
}
}
}
return _innerCatalog;
}
}
protected override void Dispose(bool disposing)
{
try
{
if (!_isDisposed)
{
AggregateCatalog? innerCatalog = null;
lock (_thisLock)
{
innerCatalog = _innerCatalog;
_innerCatalog = null;
_isDisposed = true;
}
innerCatalog?.Dispose();
}
}
finally
{
base.Dispose(disposing);
}
}
public override IEnumerator<ComposablePartDefinition> GetEnumerator()
{
ThrowIfDisposed();
return InnerCatalog.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="DirectoryCatalog"/> has been disposed of.
/// </exception>
public override IEnumerable<Tuple<ComposablePartDefinition, ExportDefinition>> GetExports(ImportDefinition definition)
{
ThrowIfDisposed();
Requires.NotNull(definition, nameof(definition));
return InnerCatalog.GetExports(definition);
}
[DebuggerStepThrough]
private void ThrowIfDisposed()
{
if (_isDisposed)
{
throw ExceptionBuilder.CreateObjectDisposed(this);
}
}
private string GetDisplayName() =>
$"{GetType().Name} (Path=\"{AppDomain.CurrentDomain.BaseDirectory}\") (PrivateProbingPath=\"{AppDomain.CurrentDomain.RelativeSearchPath}\")"; // NOLOC
/// <summary>
/// Returns a string representation of the directory catalog.
/// </summary>
/// <returns>
/// A <see cref="string"/> containing the string representation of the <see cref="DirectoryCatalog"/>.
/// </returns>
public override string ToString()
{
return GetDisplayName();
}
/// <summary>
/// Gets the display name of the ApplicationCatalog.
/// </summary>
/// <value>
/// A <see cref="string"/> containing a human-readable display name of the <see cref="ApplicationCatalog"/>.
/// </value>
string ICompositionElement.DisplayName
{
get { return GetDisplayName(); }
}
/// <summary>
/// Gets the composition element from which the ApplicationCatalog originated.
/// </summary>
/// <value>
/// This property always returns <see langword="null"/>.
/// </value>
ICompositionElement? ICompositionElement.Origin
{
get { return null; }
}
}
}