-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ImportConventionBuilder.cs
195 lines (171 loc) · 7.45 KB
/
ImportConventionBuilder.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
// 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;
namespace System.Composition.Convention
{
/// <summary>
/// Configures an import associated with a part.
/// </summary>
public sealed class ImportConventionBuilder
{
private static readonly Type[] s_supportedImportManyTypes = new[] { typeof(IList<>), typeof(ICollection<>), typeof(IEnumerable<>) };
private string _contractName;
private bool? _asMany;
private bool _allowDefault;
private Func<Type, string> _getContractNameFromPartType;
private List<Tuple<string, object>> _metadataConstraintItems;
private List<Tuple<string, Func<Type, object>>> _metadataConstraintItemFuncs;
internal ImportConventionBuilder() { }
/// <summary>
/// Specify the contract name for the import.
/// </summary>
/// <param name="contractName"></param>
/// <returns>An import builder allowing further configuration.</returns>
public ImportConventionBuilder AsContractName(string contractName)
{
if (contractName is null)
{
throw new ArgumentNullException(nameof(contractName));
}
if (contractName.Length == 0)
{
throw new ArgumentException(SR.Format(SR.ArgumentException_EmptyString, nameof(contractName)), nameof(contractName));
}
_contractName = contractName;
return this;
}
/// <summary>
/// Specify the contract name for the export.
/// </summary>
/// <param name="getContractNameFromPartType">A Func to retrieve the contract name from the part typeThe contract name.</param>
/// <returns>An export builder allowing further configuration.</returns>
public ImportConventionBuilder AsContractName(Func<Type, string> getContractNameFromPartType)
{
if (getContractNameFromPartType is null)
{
throw new ArgumentNullException(nameof(getContractNameFromPartType));
}
_getContractNameFromPartType = getContractNameFromPartType;
return this;
}
/// <summary>
/// Configure the import to receive all exports of the contract.
/// </summary>
/// <returns>An import builder allowing further configuration.</returns>
public ImportConventionBuilder AsMany()
{
return AsMany(true);
}
/// <summary>
/// Configure the import to receive all exports of the contract.
/// </summary>
/// <param name="isMany">True if the import receives all values; otherwise false.</param>
/// <returns>An import builder allowing further configuration.</returns>
public ImportConventionBuilder AsMany(bool isMany)
{
_asMany = isMany;
return this;
}
/// <summary>
/// Allow the import to receive the default value for its type if
/// the contract cannot be supplied by another part.
/// </summary>
/// <returns>An import builder allowing further configuration.</returns>
public ImportConventionBuilder AllowDefault()
{
_allowDefault = true;
return this;
}
/// <summary>
/// Add an import constraint
/// </summary>
/// <param name="name">The name of the constraint item.</param>
/// <param name="value">The value to match.</param>
/// <returns>An import builder allowing further configuration.</returns>
public ImportConventionBuilder AddMetadataConstraint(string name, object value)
{
if (name is null)
{
throw new ArgumentNullException(nameof(name));
}
if (name.Length == 0)
{
throw new ArgumentException(SR.Format(SR.ArgumentException_EmptyString, nameof(name)), nameof(name));
}
_metadataConstraintItems ??= new List<Tuple<string, object>>();
_metadataConstraintItems.Add(Tuple.Create(name, value));
return this;
}
/// <summary>
/// Add an import constraint
/// </summary>
/// <param name="name">The name of the constraint item.</param>
/// <param name="getConstraintValueFromPartType">A function that calculates the value to match.</param>
/// <returns>An export builder allowing further configuration.</returns>
public ImportConventionBuilder AddMetadataConstraint(string name, Func<Type, object> getConstraintValueFromPartType)
{
if (name is null)
{
throw new ArgumentNullException(nameof(name));
}
if (getConstraintValueFromPartType is null)
{
throw new ArgumentNullException(nameof(getConstraintValueFromPartType));
}
if (name.Length == 0)
{
throw new ArgumentException(SR.Format(SR.ArgumentException_EmptyString, nameof(name)), nameof(name));
}
_metadataConstraintItemFuncs ??= new List<Tuple<string, Func<Type, object>>>();
_metadataConstraintItemFuncs.Add(Tuple.Create(name, getConstraintValueFromPartType));
return this;
}
internal void BuildAttributes(Type type, ref List<Attribute> attributes)
{
Attribute importAttribute;
var contractName = (_getContractNameFromPartType != null) ? _getContractNameFromPartType(type) : _contractName;
// Infer from Type when not explicitly set.
var asMany = _asMany ?? IsSupportedImportManyType(type.GetTypeInfo());
if (!asMany)
{
importAttribute = new ImportAttribute(contractName)
{
AllowDefault = _allowDefault
};
}
else
{
importAttribute = new ImportManyAttribute(contractName);
}
attributes ??= new List<Attribute>();
attributes.Add(importAttribute);
//Add metadata attributes from direct specification
if (_metadataConstraintItems != null)
{
foreach (Tuple<string, object> item in _metadataConstraintItems)
{
attributes.Add(new ImportMetadataConstraintAttribute(item.Item1, item.Item2));
}
}
//Add metadata attributes from func specification
if (_metadataConstraintItemFuncs != null)
{
foreach (Tuple<string, Func<Type, object>> item in _metadataConstraintItemFuncs)
{
var name = item.Item1;
var value = (item.Item2 != null) ? item.Item2(type) : null;
attributes.Add(new ImportMetadataConstraintAttribute(name, value));
}
}
return;
}
private static bool IsSupportedImportManyType(TypeInfo typeInfo)
{
return typeInfo.IsArray ||
(typeInfo.IsGenericTypeDefinition && s_supportedImportManyTypes.Contains(typeInfo.AsType())) ||
(typeInfo.AsType().IsConstructedGenericType && s_supportedImportManyTypes.Contains(typeInfo.GetGenericTypeDefinition()));
}
}
}