-
-
Notifications
You must be signed in to change notification settings - Fork 832
/
MatchingSignatureConstructorSelector.cs
125 lines (103 loc) · 4.76 KB
/
MatchingSignatureConstructorSelector.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
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System.Globalization;
namespace Autofac.Core.Activators.Reflection;
/// <summary>
/// Selects a constructor based on its signature.
/// </summary>
public class MatchingSignatureConstructorSelector : IConstructorSelector, IConstructorSelectorWithEarlyBinding
{
private readonly Type[] _signature;
/// <summary>
/// Initializes a new instance of the <see cref="MatchingSignatureConstructorSelector"/> class.
/// </summary>
/// <param name="signature">Signature to match.</param>
public MatchingSignatureConstructorSelector(params Type[] signature)
{
_signature = signature ?? throw new ArgumentNullException(nameof(signature));
}
/// <summary>
/// Selects the best constructor from the available constructors.
/// </summary>
/// <param name="constructorBindings">Available constructors.</param>
/// <param name="parameters">Parameters to the instance being resolved.</param>
/// <returns>The best constructor.</returns>
public BoundConstructor SelectConstructorBinding(BoundConstructor[] constructorBindings, IEnumerable<Parameter> parameters)
{
if (constructorBindings == null)
{
throw new ArgumentNullException(nameof(constructorBindings));
}
var matchingCount = 0;
var validCount = 0;
BoundConstructor? chosen = null;
for (var idx = 0; idx < constructorBindings.Length; idx++)
{
var binding = constructorBindings[idx];
if (binding.CanInstantiate)
{
validCount++;
// Concievably could store the set of parameter types in the binder as well, but
// that's yet more memory up-front, for a less used constructor selector.
if (binding.Binder.Parameters.Select(p => p.ParameterType).SequenceEqual(_signature))
{
chosen = binding;
matchingCount++;
}
}
}
if (matchingCount == 1)
{
return chosen!;
}
if (validCount == 0)
{
throw new ArgumentException(MatchingSignatureConstructorSelectorResources.AtLeastOneBindingRequired);
}
// DeclaringType will be non-null for constructors.
var targetTypeName = constructorBindings[0].TargetConstructor.DeclaringType!.Name;
var signature = string.Join(", ", _signature.Select(t => t.Name).ToArray());
if (matchingCount == 0)
{
throw new DependencyResolutionException(string.Format(CultureInfo.CurrentCulture, MatchingSignatureConstructorSelectorResources.RequiredConstructorNotAvailable, targetTypeName, signature));
}
throw new DependencyResolutionException(string.Format(CultureInfo.CurrentCulture, MatchingSignatureConstructorSelectorResources.TooManyConstructorsMatch, signature));
}
/// <summary>
/// Selects the best constructor from the available constructor bindings.
/// </summary>
/// <param name="constructorBinders">Available constructors.</param>
/// <returns>The best constructor.</returns>
public ConstructorBinder SelectConstructorBinder(ConstructorBinder[] constructorBinders)
{
if (constructorBinders == null)
{
throw new ArgumentNullException(nameof(constructorBinders));
}
var matchingCount = 0;
ConstructorBinder? chosen = null;
for (var idx = 0; idx < constructorBinders.Length; idx++)
{
var binding = constructorBinders[idx];
// Concievably could store the set of parameter types in the binder as well, but
// that's yet more memory up-front, for a less used constructor selector.
if (binding.Parameters.Select(p => p.ParameterType).SequenceEqual(_signature))
{
chosen = binding;
matchingCount++;
}
}
if (matchingCount == 1)
{
return chosen!;
}
// DeclaringType will be non-null for constructors.
var targetTypeName = constructorBinders[0].Constructor.DeclaringType!.Name;
var signature = string.Join(", ", _signature.Select(t => t.Name).ToArray());
if (matchingCount == 0)
{
throw new DependencyResolutionException(string.Format(CultureInfo.CurrentCulture, MatchingSignatureConstructorSelectorResources.RequiredConstructorNotAvailable, targetTypeName, signature));
}
throw new DependencyResolutionException(string.Format(CultureInfo.CurrentCulture, MatchingSignatureConstructorSelectorResources.TooManyConstructorsMatch, signature));
}
}