コード例 #1
0
        /// <summary>
        /// Selects the best constructor from the available constructors.
        /// </summary>
        /// <param name="constructorBindings">Available constructors.</param>
        /// <returns>The best constructor.</returns>
        public ConstructorParameterBinding SelectConstructorBinding(ConstructorParameterBinding[] constructorBindings)
        {
            if (constructorBindings == null) throw new ArgumentNullException("constructorBindings");

            var result = constructorBindings
                .Where(b => b.TargetConstructor.GetParameters().Select(p => p.ParameterType).SequenceEqual(_signature))
                .ToArray();

            if (result.Length == 1)
                return result[0];

            if (!constructorBindings.Any())
                throw new ArgumentException(MatchingSignatureConstructorSelectorResources.AtLeastOneBindingRequired);

            var targetTypeName = constructorBindings.First().TargetConstructor.DeclaringType.Name;
            var signature = string.Join(", ", _signature.Select(t => t.Name).ToArray());

            if (result.Length == 0)
                throw new DependencyResolutionException(string.Format(CultureInfo.CurrentCulture, MatchingSignatureConstructorSelectorResources.RequiredConstructorNotAvailable, targetTypeName, signature));

            throw new DependencyResolutionException(string.Format(CultureInfo.CurrentCulture, MatchingSignatureConstructorSelectorResources.TooManyConstructorsMatch, signature));
        }
コード例 #2
0
 /// <summary>
 /// Selects the best constructor from the available constructors.
 /// </summary>
 /// <param name="constructorBindings">Available constructors.</param>
 /// <returns>
 /// The best constructor.
 /// </returns>
 public ConstructorParameterBinding SelectConstructorBinding(ConstructorParameterBinding[] constructorBindings)
 {
     return constructorBindings.First();
 }