/
ImportBuilder.cs
102 lines (87 loc) · 2.98 KB
/
ImportBuilder.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
// 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;
using System.Collections.Generic;
namespace System.ComponentModel.Composition.Registration
{
public sealed class ImportBuilder
{
private static readonly Type s_stringType = typeof(string);
private string _contractName;
private Type _contractType;
private bool _asMany;
private bool _asManySpecified;
private bool _allowDefault;
private bool _allowRecomposition;
private CreationPolicy _requiredCreationPolicy;
private ImportSource _source;
public ImportBuilder() { }
public ImportBuilder AsContractType<T>()
{
return AsContractType(typeof(T));
}
public ImportBuilder AsContractType(Type type)
{
_contractType = type;
return this;
}
public ImportBuilder AsContractName(string contractName)
{
_contractName = contractName;
return this;
}
public ImportBuilder AsMany(bool isMany = true)
{
_asMany = isMany;
_asManySpecified = true;
return this;
}
public ImportBuilder AllowDefault()
{
_allowDefault = true;
return this;
}
public ImportBuilder AllowRecomposition()
{
_allowRecomposition = true;
return this;
}
public ImportBuilder RequiredCreationPolicy(CreationPolicy requiredCreationPolicy)
{
_requiredCreationPolicy = requiredCreationPolicy;
return this;
}
public ImportBuilder Source(ImportSource source)
{
_source = source;
return this;
}
internal void BuildAttributes(Type type, ref List<Attribute> attributes)
{
Attribute importAttribute;
// Infer from Type when not explicitly set.
bool asMany = (!_asManySpecified) ? type != s_stringType && typeof(IEnumerable).IsAssignableFrom(type) : _asMany;
if (!asMany)
{
importAttribute = new ImportAttribute(_contractName, _contractType)
{
AllowDefault = _allowDefault,
AllowRecomposition = _allowRecomposition,
RequiredCreationPolicy = _requiredCreationPolicy,
Source = _source
};
}
else
{
importAttribute = new ImportManyAttribute(_contractName, _contractType)
{
AllowRecomposition = _allowRecomposition,
RequiredCreationPolicy = _requiredCreationPolicy,
Source = _source
};
}
attributes ??= new List<Attribute>();
attributes.Add(importAttribute);
}
}
}