Beispiel #1
0
        public ConstructorInfo TryGetConstructor(Type implementationType, out string errorMessage)
        {
            errorMessage = "";
            var longestConstructor = (from constructor in implementationType.GetConstructors()
                                      orderby constructor.GetParameters().Count() descending
                                      select constructor).Take(1);

            if (longestConstructor.Any())
            {
                return(longestConstructor.First());
            }

            return(_originalBehavior.TryGetConstructor(implementationType, out errorMessage));
        }
Beispiel #2
0
        /// <summary>
        /// Gets the given <paramref name="implementationType"/>'s constructor that can be used by the
        /// container to create that instance.
        /// </summary>
        /// <param name="behavior">The behavior.</param>
        /// <param name="implementationType">Type of the implementation to find a suitable constructor for.</param>
        /// <returns>
        /// The <see cref="ConstructorInfo"/>. This method never returns null.
        /// </returns>
        /// <exception cref="ActivationException">Thrown when no suitable constructor could be found.</exception>
        public static ConstructorInfo GetConstructor(
            this IConstructorResolutionBehavior behavior, Type implementationType)
        {
            // NOTE: In v5, both IConstructorResolutionBehavior's and IDependencyInjectionBehavior's method
            // signatures changed (see #557). This is both a binary and code breaking change, which affects
            // anyone implementing an IConstructorResolutionBehavior. There's nothing much we can do about
            // this (since fixing #557 was more important). By adding this extension method, however, we can
            // reduce the pain for anyone using (calling) this interface. This extension method duplicates the
            // signature and behavior of the old method. This still makes the change binary incompatible, even
            // from perspective of the caller, but it would allow their code to keep compiling (in most cases).
            Requires.IsNotNull(behavior, nameof(behavior));
            Requires.IsNotNull(implementationType, nameof(implementationType));

            return(behavior.TryGetConstructor(implementationType, out string?message)
                   ?? throw BuildActivationException(behavior, implementationType, message));
        }