-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
ConventionBuilder.cs
395 lines (351 loc) · 16.5 KB
/
ConventionBuilder.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// 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.Linq;
using System.Reflection;
using System.Threading;
namespace System.Composition.Convention
{
/// <summary>
/// Entry point for defining rules that configure plain-old-CLR-objects as MEF parts.
/// </summary>
public class ConventionBuilder : AttributedModelProvider
{
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
private readonly List<PartConventionBuilder> _conventions = new List<PartConventionBuilder>();
private readonly Dictionary<MemberInfo, List<Attribute>> _memberInfos = new Dictionary<MemberInfo, List<Attribute>>();
private readonly Dictionary<ParameterInfo, List<Attribute>> _parameters = new Dictionary<ParameterInfo, List<Attribute>>();
/// <summary>
/// Construct a new <see cref="ConventionBuilder"/>.
/// </summary>
public ConventionBuilder()
{
}
/// <summary>
/// Define a rule that will apply to all types that
/// derive from (or implement) the specified type.
/// </summary>
/// <typeparam name="T">The type from which matching types derive.</typeparam>
/// <returns>A <see cref="PartConventionBuilder{T}"/> that must be used to specify the rule.</returns>
public PartConventionBuilder<T> ForTypesDerivedFrom<T>()
{
var partBuilder = new PartConventionBuilder<T>((t) => IsDescendentOf(t, typeof(T)));
_conventions.Add(partBuilder);
return partBuilder;
}
/// <summary>
/// Define a rule that will apply to all types that
/// derive from (or implement) the specified type.
/// </summary>
/// <param name="type">The type from which matching types derive.</param>
/// <returns>A <see cref="PartConventionBuilder"/> that must be used to specify the rule.</returns>
public PartConventionBuilder ForTypesDerivedFrom(Type type)
{
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}
var partBuilder = new PartConventionBuilder((t) => IsDescendentOf(t, type));
_conventions.Add(partBuilder);
return partBuilder;
}
/// <summary>
/// Define a rule that will apply to the types <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type to which the rule applies.</typeparam>
/// <returns>A <see cref="PartConventionBuilder{T}"/> that must be used to specify the rule.</returns>
public PartConventionBuilder<T> ForType<T>()
{
var partBuilder = new PartConventionBuilder<T>((t) => t == typeof(T));
_conventions.Add(partBuilder);
return partBuilder;
}
/// <summary>
/// Define a rule that will apply to the types <paramref name="type"/>.
/// </summary>
/// <param name="type">The type to which the rule applies.</param>
/// <returns>A <see cref="PartConventionBuilder"/> that must be used to specify the rule.</returns>
public PartConventionBuilder ForType(Type type)
{
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}
var partBuilder = new PartConventionBuilder((t) => t == type);
_conventions.Add(partBuilder);
return partBuilder;
}
/// <summary>
/// Define a rule that will apply to types assignable to <typeparamref name="T"/> that
/// match the supplied predicate.
/// </summary>
/// <param name="typeFilter">A predicate that selects matching types.</param>
/// <typeparam name="T">The type to which the rule applies.</typeparam>
/// <returns>A <see cref="PartConventionBuilder{T}"/> that must be used to specify the rule.</returns>
public PartConventionBuilder<T> ForTypesMatching<T>(Predicate<Type> typeFilter)
{
if (typeFilter is null)
{
throw new ArgumentNullException(nameof(typeFilter));
}
var partBuilder = new PartConventionBuilder<T>(typeFilter);
_conventions.Add(partBuilder);
return partBuilder;
}
/// <summary>
/// Define a rule that will apply to types that
/// match the supplied predicate.
/// </summary>
/// <param name="typeFilter">A predicate that selects matching types.</param>
/// <returns>A <see cref="PartConventionBuilder{T}"/> that must be used to specify the rule.</returns>
public PartConventionBuilder ForTypesMatching(Predicate<Type> typeFilter)
{
if (typeFilter is null)
{
throw new ArgumentNullException(nameof(typeFilter));
}
var partBuilder = new PartConventionBuilder(typeFilter);
_conventions.Add(partBuilder);
return partBuilder;
}
private IEnumerable<Tuple<object, List<Attribute>>> EvaluateThisTypeInfoAgainstTheConvention(TypeInfo typeInfo)
{
List<Attribute> attributes = new List<Attribute>();
var configuredMembers = new List<Tuple<object, List<Attribute>>>();
bool specifiedConstructor = false;
bool matchedConvention = false;
Type type = typeInfo.AsType();
foreach (PartConventionBuilder builder in _conventions.Where(c => c.SelectType(type)))
{
attributes.AddRange(builder.BuildTypeAttributes(type));
specifiedConstructor |= builder.BuildConstructorAttributes(type, ref configuredMembers);
builder.BuildPropertyAttributes(type, ref configuredMembers);
builder.BuildOnImportsSatisfiedNotification(type, ref configuredMembers);
matchedConvention = true;
}
if (matchedConvention && !specifiedConstructor)
{
// DefaultConstructor
PartConventionBuilder.BuildDefaultConstructorAttributes(type, ref configuredMembers);
}
configuredMembers.Add(Tuple.Create((object)type.GetTypeInfo(), attributes));
return configuredMembers;
}
/// <summary>
/// Provide the list of attributes applied to the specified member.
/// </summary>
/// <param name="reflectedType">The reflectedType the type used to retrieve the memberInfo.</param>
/// <param name="member">The member to supply attributes for.</param>
/// <returns>The list of applied attributes.</returns>
public override IEnumerable<Attribute> GetCustomAttributes(Type reflectedType, System.Reflection.MemberInfo member)
{
if (member is null)
{
throw new ArgumentNullException(nameof(member));
}
// Now edit the attributes returned from the base type
List<Attribute> cachedAttributes = null;
var typeInfo = member as TypeInfo;
if (typeInfo != null)
{
var memberInfo = typeInfo as MemberInfo;
_lock.EnterReadLock();
try
{
_memberInfos.TryGetValue(memberInfo, out cachedAttributes);
}
finally
{
_lock.ExitReadLock();
}
if (cachedAttributes == null)
{
_lock.EnterWriteLock();
try
{
//Double check locking another thread may have inserted one while we were away.
if (!_memberInfos.TryGetValue(memberInfo, out cachedAttributes))
{
List<Attribute> attributeList;
foreach (Tuple<object, List<Attribute>> element in EvaluateThisTypeInfoAgainstTheConvention(typeInfo))
{
attributeList = element.Item2;
if (attributeList != null)
{
var mi = element.Item1 as MemberInfo;
if (mi != null)
{
if (mi != null && (mi is ConstructorInfo || mi is TypeInfo || mi is PropertyInfo || mi is MethodInfo))
{
if (!_memberInfos.TryGetValue(mi, out List<Attribute> memberAttributes))
{
_memberInfos.Add(mi, element.Item2);
}
}
}
else
{
var pi = element.Item1 as ParameterInfo;
if (pi == null)
{
throw new Exception(SR.Diagnostic_InternalExceptionMessage);
}
// Item contains as Constructor parameter to configure
if (!_parameters.TryGetValue(pi, out List<Attribute> parameterAttributes))
{
_parameters.Add(pi, element.Item2);
}
}
}
}
}
// We will have updated all of the MemberInfos by now so lets reload cachedAttributes with the current store
_memberInfos.TryGetValue(memberInfo, out cachedAttributes);
}
finally
{
_lock.ExitWriteLock();
}
}
}
else if (member is PropertyInfo || member is ConstructorInfo || member is MethodInfo)
{
cachedAttributes = ReadMemberCustomAttributes(reflectedType, member);
}
IEnumerable<Attribute> appliedAttributes;
if (!(member is TypeInfo) && member.DeclaringType != reflectedType)
appliedAttributes = Enumerable.Empty<Attribute>();
else
appliedAttributes = member.GetCustomAttributes<Attribute>(false);
return cachedAttributes == null ? appliedAttributes : appliedAttributes.Concat(cachedAttributes);
}
private List<Attribute> ReadMemberCustomAttributes(Type reflectedType, System.Reflection.MemberInfo member)
{
List<Attribute> cachedAttributes = null;
bool getMemberAttributes = false;
// Now edit the attributes returned from the base type
_lock.EnterReadLock();
try
{
if (!_memberInfos.TryGetValue(member, out cachedAttributes))
{
// If there is nothing for this member Cache any attributes for the DeclaringType
if (reflectedType != null
&& !_memberInfos.TryGetValue(member.DeclaringType.GetTypeInfo() as MemberInfo, out cachedAttributes))
{
// If there is nothing for this parameter look to see if the declaring Member has been cached yet?
// need to do it outside of the lock, so set the flag we'll check it in a bit
getMemberAttributes = true;
}
cachedAttributes = null;
}
}
finally
{
_lock.ExitReadLock();
}
if (getMemberAttributes)
{
GetCustomAttributes(null, reflectedType.GetTypeInfo() as MemberInfo);
// We should have run the rules for the enclosing parameter so we can again
_lock.EnterReadLock();
try
{
_memberInfos.TryGetValue(member, out cachedAttributes);
}
finally
{
_lock.ExitReadLock();
}
}
return cachedAttributes;
}
/// <summary>
/// Provide the list of attributes applied to the specified parameter.
/// </summary>
/// <param name="reflectedType">The reflectedType the type used to retrieve the parameterInfo.</param>
/// <param name="parameter">The parameter to supply attributes for.</param>
/// <returns>The list of applied attributes.</returns>
public override IEnumerable<Attribute> GetCustomAttributes(Type reflectedType, System.Reflection.ParameterInfo parameter)
{
if (parameter is null)
{
throw new ArgumentNullException(nameof(parameter));
}
IEnumerable<Attribute> attributes = parameter.GetCustomAttributes<Attribute>(false);
List<Attribute> cachedAttributes = ReadParameterCustomAttributes(reflectedType, parameter);
return cachedAttributes == null ? attributes : attributes.Concat(cachedAttributes);
}
private List<Attribute> ReadParameterCustomAttributes(Type reflectedType, System.Reflection.ParameterInfo parameter)
{
List<Attribute> cachedAttributes = null;
bool getMemberAttributes = false;
// Now edit the attributes returned from the base type
_lock.EnterReadLock();
try
{
if (!_parameters.TryGetValue(parameter, out cachedAttributes))
{
// If there is nothing for this parameter Cache any attributes for the DeclaringType
if (reflectedType != null
&& !_memberInfos.TryGetValue(reflectedType.GetTypeInfo() as MemberInfo, out cachedAttributes))
{
// If there is nothing for this parameter look to see if the declaring Member has been cached yet?
// need to do it outside of the lock, so set the flag we'll check it in a bit
getMemberAttributes = true;
}
cachedAttributes = null;
}
}
finally
{
_lock.ExitReadLock();
}
if (getMemberAttributes)
{
GetCustomAttributes(null, reflectedType.GetTypeInfo() as MemberInfo);
// We should have run the rules for the enclosing parameter so we can again
_lock.EnterReadLock();
try
{
_parameters.TryGetValue(parameter, out cachedAttributes);
}
finally
{
_lock.ExitReadLock();
}
}
return cachedAttributes;
}
private static bool IsGenericDescendentOf(TypeInfo derivedType, TypeInfo baseType)
{
if (derivedType.BaseType == null)
return false;
if (derivedType.BaseType == baseType.AsType())
return true;
foreach (Type iface in derivedType.ImplementedInterfaces)
{
if (iface.IsConstructedGenericType &&
iface.GetGenericTypeDefinition() == baseType.AsType())
return true;
}
return IsGenericDescendentOf(derivedType.BaseType.GetTypeInfo(), baseType);
}
private static bool IsDescendentOf(Type type, Type baseType)
{
if (type == baseType || type == typeof(object) || type == null)
{
return false;
}
TypeInfo ti = type.GetTypeInfo();
TypeInfo bti = baseType.GetTypeInfo();
// The baseType can be an open generic, in that case this ensures
// that the derivedType is checked against it
if (ti.IsGenericTypeDefinition || bti.IsGenericTypeDefinition)
{
return IsGenericDescendentOf(ti, bti);
}
return bti.IsAssignableFrom(ti);
}
}
}