Binds a constructor to the parameters that will be used when it is invoked.
        /// <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));
        }
Esempio n. 2
0
        ConstructorParameterBinding[] GetConstructorBindings(
            IComponentContext context,
            IEnumerable <Parameter> parameters,
            IEnumerable <Parameter> _defaultParameters,
            ConstructorInfo[] constructorInfo)
        {
            var prioritisedParameters = parameters.Concat(_defaultParameters).ToArray();

            var result = new ConstructorParameterBinding[constructorInfo.Length];

            for (int i = 0; i < constructorInfo.Length; i++)
            {
                result[i] = new ConstructorParameterBinding(constructorInfo[i], prioritisedParameters, context);
            }
            return(result);
        }
        /// <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("At least one binding must be provided in order to select a constructor.");

            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("The required constructor on type '{0}'  with signature '{1}' is unavailable.", targetTypeName, signature));

            throw new DependencyResolutionException(string.Format("More than one constructor matches the signature '{0}'.", signature));
        }
        /// <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(MatchingSignatureConstructorSelectorResources.RequiredConstructorNotAvailable, targetTypeName, signature));
            
            throw new DependencyResolutionException(string.Format(MatchingSignatureConstructorSelectorResources.TooManyConstructorsMatch, signature));
        }
Esempio n. 5
0
		ConstructorParameterBinding[] GetConstructorBindings(
			IComponentContext context,
			IEnumerable<Parameter> parameters,
			IEnumerable<Parameter> _defaultParameters,
			ConstructorInfo[] constructorInfo)
		{
			var prioritisedParameters = parameters.Concat(_defaultParameters).ToArray();

			var result = new ConstructorParameterBinding[constructorInfo.Length];
			for (int i = 0; i < constructorInfo.Length; i++)
				result[i] = new ConstructorParameterBinding(constructorInfo[i], prioritisedParameters, context);
			return result;
		}