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
|
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
//
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Threading;
namespace Microsoft.VisualStudio.Utilities
{
/// <summary>
/// Operations that guard calls to extensions code, track performance and log errors.
/// </summary>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
public interface IGuardedOperations
{
/// <summary>
/// Makes a guarded call to an extension point.
/// </summary>
/// <param name="call">Delegate that calls the extension point.</param>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
void CallExtensionPoint(Action call);
/// <summary>
/// Makes a guarded call to an extension point.
/// </summary>
/// <param name="errorSource">Reference to the extension object or event handler that may throw an exception.
/// Used for tracking performance and errors.</param>
/// <param name="call">Delegate that calls the extension point.</param>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
void CallExtensionPoint(object errorSource, Action call);
/// <summary>
/// Makes a guarded call to an extension point.
/// </summary>
/// <param name="errorSource">Reference to the extension object or event handler that may throw an exception.
/// Used for tracking performance and errors.</param>
/// <param name="call">Delegate that calls the extension point.</param>
/// <param name="exceptionGuardFilter">Determines which exceptions should be guarded against.
/// An exception gets handled only if <paramref name="exceptionGuardFilter"/> returns <c>true</c>.</param>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
void CallExtensionPoint(object errorSource, Action call, Predicate<Exception> exceptionGuardFilter);
/// <summary>
/// Makes a guarded call to an extension point.
/// </summary>
/// <param name="call">Delegate that calls the extension point.</param>
/// <param name="valueOnThrow">The value returned if the delegate call failed.</param>
/// <returns>The result of the <paramref name="call"/> or <paramref name="valueOnThrow"/>.</returns>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
T CallExtensionPoint<T>(Func<T> call, T valueOnThrow);
/// <summary>
/// Makes a guarded call to an extension point.
/// </summary>
/// <param name="errorSource">Reference to the extension object or event handler that may throw an exception.
/// Used for tracking performance and errors.</param>
/// <param name="call">Delegate that calls the extension point.</param>
/// <param name="valueOnThrow">The value returned if the delegate call failed.</param>
/// <returns>The result of the <paramref name="call"/> or <paramref name="valueOnThrow"/>.</returns>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
T CallExtensionPoint<T>(object errorSource, Func<T> call, T valueOnThrow);
/// <summary>
/// Makes a guarded call to an async extension point.
/// </summary>
/// <param name="asyncCall">Delegate that calls the extension point.</param>
/// <returns>A <see cref="Task"/> that asynchronously executes the <paramref name="asyncAction"/>.</returns>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
Task CallExtensionPointAsync(Func<Task> asyncAction);
/// <summary>
/// Makes a guarded call to an async extension point.
/// </summary>
/// <param name="errorSource">Reference to the extension object or event handler that may throw an exception.
/// Used for tracking performance and errors.</param>
/// <param name="asyncCall">Delegate that calls the extension point.</param>
/// <returns>A <see cref="Task"/> that asynchronously executes the <paramref name="asyncAction"/>.</returns>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
Task CallExtensionPointAsync(object errorSource, Func<Task> asyncAction);
/// <summary>
/// Makes a guarded call to an async extension point.
/// </summary>
/// <typeparam name="T">The type of the value returned from the <paramref name="asyncCall"/>.</typeparam>
/// <param name="asyncCall">Delegate that calls the extension point.</param>
/// <param name="valueOnThrow">The value returned if the delegate call failed.</param>
/// <returns>A <see cref="Task{T}"/> that asynchronously executes the <paramref name="asyncCall"/> or provides <paramref name="valueOnThrow"/>.</returns>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
Task<T> CallExtensionPointAsync<T>(Func<Task<T>> asyncCall, T valueOnThrow);
/// <summary>
/// Makes a guarded call to an async extension point.
/// </summary>
/// <typeparam name="T">The type of the value returned from the <paramref name="asyncCall"/>.</typeparam>
/// <param name="errorSource">Reference to the extension object or event handler that may throw an exception.
/// Used for tracking performance and errors.</param>
/// <param name="asyncCall">Delegate that calls the extension point.</param>
/// <param name="valueOnThrow">The value returned if the delegate call failed.</param>
/// <returns>A <see cref="Task{T}"/> that asynchronously executes the <paramref name="asyncCall"/> or provides <paramref name="valueOnThrow"/>.</returns>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
Task<T> CallExtensionPointAsync<T>(object errorSource, Func<Task<T>> asyncCall, T valueOnThrow);
/// <summary>
/// Selects extension factories whose declared content type metadata
/// matches the provided target content type, taking into account that extension factory
/// may be disabled by a Replace attribute on another factory.
/// </summary>
/// <param name="lazyFactories">Lazy references that will be evaluated.</param>
/// <param name="dataContentType">Target content type.</param>
/// <param name="contentTypeRegistryService">Instance of <see cref="IContentTypeRegistryService"/> which orders content types.</param>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
IEnumerable<Lazy<TExtensionFactory, TMetadataView>> FindEligibleFactories<TExtensionFactory, TMetadataView>(IEnumerable<Lazy<TExtensionFactory, TMetadataView>> lazyFactories, IContentType dataContentType, IContentTypeRegistryService contentTypeRegistryService)
where TExtensionFactory : class
where TMetadataView : INamedContentTypeMetadata;
/// <summary>
/// Handles an exception occured in a call to an extension point.
/// </summary>
/// <param name="errorSource">Reference to the extension object or event handler that threw the exception</param>
/// <param name="e">Exception to handle</param>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
void HandleException(object errorSource, Exception e);
/// <summary>
/// Safely instantiates an extension point.
/// </summary>
/// <param name="errorSource">Reference to the object that will be blamed for potential exceptions.</param>
/// <param name="provider">Lazy reference that will be initialized.</param>
/// <returns>Initialized instance stored in <paramref name="provider"/>.</returns>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
TExtension InstantiateExtension<TExtension>(object errorSource, Lazy<TExtension> provider);
/// <summary>
/// Safely instantiates an extension point.
/// </summary>
/// <param name="errorSource">Reference to the object that will be blamed for potential exceptions.</param>
/// <param name="provider">Lazy reference that will be initialized.</param>
/// <returns>Initialized instance stored in <paramref name="provider"/>.</returns>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
TExtension InstantiateExtension<TExtension, TMetadata>(object errorSource, Lazy<TExtension, TMetadata> provider);
/// <summary>
/// Safely invokes a delegate on the extension point.
/// </summary>
/// <param name="errorSource">Reference to the object that will be blamed for potential exceptions.</param>
/// <param name="provider">Lazy reference that will be initialized.</param>
/// <param name="getter">Delegate which constructs an instance of the extension from its <paramref name="provider"/>.</param>
/// <returns>The result of <paramref name="getter"/>.</returns>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
TExtensionInstance InstantiateExtension<TExtension, TMetadata, TExtensionInstance>(object errorSource, Lazy<TExtension, TMetadata> provider, Func<TExtension, TExtensionInstance> getter);
/// <summary>
/// Safely instantiates an extension point whose declared content type metadata
/// is the closest match to the provided target content type.
/// </summary>
/// <param name="providerHandles">Lazy references that will be evaluated.</param>
/// <param name="dataContentType">Target content type.</param>
/// <param name="contentTypeRegistryService">Instance of <see cref="IContentTypeRegistryService"/> which orders content types.</param>
/// <param name="errorSource">Reference to the object that will be blamed for potential exceptions.</param>
/// <returns>The selected element of <paramref name="providerHandles"/>.</returns>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
TExtension InvokeBestMatchingFactory<TExtension, TMetadataView>(IList<Lazy<TExtension, TMetadataView>> providerHandles, IContentType dataContentType, IContentTypeRegistryService contentTypeRegistryService, object errorSource) where TMetadataView : IContentTypeMetadata;
/// <summary>
/// Safely invokes a delegate on the extension factory whose declared content type metadata
/// is the best match to the provided target content type.
/// </summary>
/// <param name="providerHandles">Lazy references that will be evaluated.</param>
/// <param name="dataContentType">Target content type.</param>
/// <param name="getter">Delegate which constructs an instance of the extension from the best matching element of <paramref name="providerHandles"/>.</param>
/// <param name="contentTypeRegistryService">Instance of <see cref="IContentTypeRegistryService"/> which orders content types.</param>
/// <param name="errorSource">Reference to the object that will be blamed for potential exceptions.</param>
/// <returns>The result of <paramref name="getter"/>.</returns>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
TExtensionInstance InvokeBestMatchingFactory<TExtensionFactory, TExtensionInstance, TMetadataView>(IList<Lazy<TExtensionFactory, TMetadataView>> providerHandles, IContentType dataContentType, Func<TExtensionFactory, TExtensionInstance> getter, IContentTypeRegistryService contentTypeRegistryService, object errorSource)
where TExtensionFactory : class
where TMetadataView : IContentTypeMetadata;
/// <summary>
/// Safely invokes a delegate on all extension factories whose declared content type metadata
/// matches the provided target content type, taking into account that extension factory
/// may be disabled by a Replace attribute on another factory.
/// </summary>
/// <param name="lazyFactories">Lazy references that will be evaluated.</param>
/// <param name="getter">Delegate which constructs an instance of the extension from each element of <paramref name="lazyFactories"/>.</param>
/// <param name="dataContentType">Target content type.</param>
/// <param name="contentTypeRegistryService">Instance of <see cref="IContentTypeRegistryService"/> which orders content types.</param>
/// <param name="errorSource">Reference to the object that will be blamed for potential exceptions.</param>
/// <returns>The list of results of <paramref name="getter"/>.</returns>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
List<TExtensionInstance> InvokeEligibleFactories<TExtensionInstance, TExtensionFactory, TMetadataView>(IEnumerable<Lazy<TExtensionFactory, TMetadataView>> lazyFactories, Func<TExtensionFactory, TExtensionInstance> getter, IContentType dataContentType, IContentTypeRegistryService contentTypeRegistryService, object errorSource)
where TExtensionInstance : class
where TExtensionFactory : class
where TMetadataView : INamedContentTypeMetadata;
/// <summary>
/// Safely invokes a delegate on all extension factories whose declared content type metadata
/// matches the provided target content type.
/// </summary>
/// <param name="lazyFactories">Lazy references that will be evaluated.</param>
/// <param name="getter">Delegate which constructs an instance of the extension from each element of <paramref name="lazyFactories"/>.</param>
/// <param name="dataContentType">Target content type.</param>
/// <param name="errorSource">Reference to the object that will be blamed for potential exceptions.</param>
/// <returns>The list of results of <paramref name="getter"/>.</returns>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
List<TExtensionInstance> InvokeMatchingFactories<TExtensionInstance, TExtensionFactory, TMetadataView>(IEnumerable<Lazy<TExtensionFactory, TMetadataView>> lazyFactories, Func<TExtensionFactory, TExtensionInstance> getter, IContentType dataContentType, object errorSource)
where TExtensionInstance : class
where TExtensionFactory : class
where TMetadataView : IContentTypeMetadata;
#pragma warning disable CA1030 // Use events where appropriate
/// <summary>
/// Safely raises an event with empty <see cref="EventArgs"/>.
/// Errors are tracked per sender, performance is tracked per handler.
/// </summary>
/// <param name="sender">Reference to the sender of the event. Tracks errors.</param>
/// <param name="eventHandlers">Event to raise. Each handler tracks performance.</param>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
void RaiseEvent(object sender, EventHandler eventHandlers);
/// <summary>
/// Safely raises an event with specified <paramref name="args"/>.
/// Errors are tracked per sender, performance is tracked per handler.
/// </summary>
/// <param name="sender">Reference to the sender of the event. Tracks errors.</param>
/// <param name="eventHandlers">Event to raise. Each handler tracks performance.</param>
/// <param name="args">Event data.</param>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
void RaiseEvent<TArgs>(object sender, EventHandler<TArgs> eventHandlers, TArgs args) where TArgs : EventArgs;
/// <summary>
/// Safely raises an event on a background thread with specified <paramref name="args"/>.
/// Errors are tracked per sender, performance is tracked per handler.
/// </summary>
/// <param name="sender">Reference to the sender of the event. Tracks errors.</param>
/// <param name="eventHandlers">Event to raise. Each handler tracks performance.</param>
/// <param name="args">Event data.</param>
/// <returns>A <see cref="Task"/> that asynchronously executes the <paramref name="eventHandlers"/>.</returns>
/// <remarks>This class supports the Visual Studio
/// infrastructure and in general is not intended to be used directly from your code.</remarks>
Task RaiseEventOnBackgroundAsync<TArgs>(object sender, AsyncEventHandler<TArgs> eventHandlers, TArgs args) where TArgs : EventArgs;
/// <summary>
/// Safely attempts to cast the given object to the given type.
/// </summary>
/// <typeparam name="TArgs">The type that should be casted to.</typeparam>
/// <param name="toCast">The object that should be casted.</param>
/// <param name="casted">Returns out the casted object or default(TArgs) if the cast failed.</param>
/// <returns>True if successful in casting, false otherwise.</returns>
bool TryCastToType<TArgs>(object toCast, out TArgs casted);
#pragma warning restore CA1030 // Use events where appropriate
}
}
|