/// <summary> /// Selects the best constructor from the available constructors. /// </summary> /// <param name="constructorBindings">Available constructors.</param> /// <returns>The best constructor.</returns> /// <exception cref='DependencyResolutionException'>A single unambiguous match could not be chosen.</exception> public ConstructorParameterBinding SelectConstructorBinding(ConstructorParameterBinding[] constructorBindings) { if (constructorBindings == null) throw new ArgumentNullException("constructorBindings"); if (constructorBindings.Length == 0) throw new ArgumentOutOfRangeException("constructorBindings"); if (constructorBindings.Length == 1) return constructorBindings[0]; var withLength = constructorBindings .Select(binding => new { Binding = binding, ConstructorParameterLength = binding.TargetConstructor.GetParameters().Length }); var maxLength = withLength.Max(binding => binding.ConstructorParameterLength); var maximal = withLength .Where(binding => binding.ConstructorParameterLength == maxLength) .Select(ctor => ctor.Binding) .ToArray(); if (maximal.Length == 1) return maximal[0]; throw new DependencyResolutionException(string.Format( CultureInfo.CurrentCulture, MostParametersConstructorSelectorResources.UnableToChooseFromMultipleConstructors, maxLength, maximal[0].TargetConstructor.DeclaringType)); }
/// <summary> /// Selects the best constructor from the available constructors. /// </summary> /// <param name="constructorBindings">Available constructors.</param> /// <returns>The best constructor.</returns> /// <exception cref='DependencyResolutionException'>A single unambiguous match could not be chosen.</exception> public ConstructorParameterBinding SelectConstructorBinding(ConstructorParameterBinding[] constructorBindings) { if (constructorBindings == null) throw new ArgumentNullException("constructorBindings"); if (constructorBindings.Length == 0) throw new ArgumentOutOfRangeException("constructorBindings"); if (constructorBindings.Length == 1) return constructorBindings[0]; var withLength = constructorBindings .Select(binding => new { Binding = binding, ConstructorParameterLength = binding.TargetConstructor.GetParameters().Length }); var maxLength = withLength.Max(binding => binding.ConstructorParameterLength); var maximal = withLength .Where(binding => binding.ConstructorParameterLength == maxLength) .Select(ctor => ctor.Binding) .ToArray(); if (maximal.Length == 1) return maximal[0]; throw new DependencyResolutionException(string.Format( "Cannot choose between multiple constructors with equal length {0} on type '{1}'. Select the constructor explicitly, with the UsingConstructor() configuration method, when the component is registered.", maxLength, maximal[0].TargetConstructor.DeclaringType)); }