/// <summary>
        ///   Gets the given <paramref name="implementationType" />'s constructor that can be used by the
        ///   container to create that instance.
        /// </summary>
        /// <param name="serviceType">Type of the abstraction that is requested.</param>
        /// <param name="implementationType">Type of the implementation to find a suitable constructor for.</param>
        /// <returns>
        ///   The <see cref="T:System.Reflection.ConstructorInfo" />.
        /// </returns>
        /// <exception cref="T:SimpleInjector.ActivationException">Thrown when no suitable constructor could be found.</exception>
        public ConstructorInfo GetConstructor(Type serviceType, Type implementationType)
        {
            // note: this I want in the future to be pushed out to a rule overrides folder
            if (typeof(EntitiesContext).IsAssignableFrom(implementationType))
            {
                var defaultconstructor = (from constructor in implementationType.GetConstructors()
                                          where constructor.GetParameters().Count() == 0
                                          select constructor).Take(1);

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

            if (serviceType.IsAssignableFrom(implementationType))
            {
                var longestConstructor = (from constructor in implementationType.GetConstructors()
                                          orderby constructor.GetParameters().Count() descending
                                          select constructor).Take(1);

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

            // fall back to the container's default behavior.
            return(_originalBehavior.GetConstructor(serviceType, implementationType));
        }
        public ConstructorInfo GetConstructor(Type serviceType, Type impType)
        {
            if (typeof(IController).IsAssignableFrom(impType))
            {
                var nonDefaultConstructor = impType.GetConstructors().Where(x => x.GetParameters().Length > 0).ToList();

                if (nonDefaultConstructor.Count == 1)
                {
                    return(nonDefaultConstructor.Single());
                }
            }

            // fall back to the container's default behavior.
            return(_defaultBehavior.GetConstructor(serviceType, impType));
        }
Beispiel #3
0
        /// <summary>
        ///   Gets the given <paramref name="implementationType" />'s constructor that can be used by the
        ///   container to create that instance.
        /// </summary>
        /// <param name="serviceType">Type of the abstraction that is requested.</param>
        /// <param name="implementationType">Type of the implementation to find a suitable constructor for.</param>
        /// <returns>
        ///   The <see cref="T:System.Reflection.ConstructorInfo" />.
        /// </returns>
        /// <exception cref="T:SimpleInjector.ActivationException">Thrown when no suitable constructor could be found.</exception>
        public ConstructorInfo GetConstructor(Type serviceType, Type implementationType)
        {
            if (serviceType.IsAssignableFrom(implementationType))
            {
                var longestConstructor = (from constructor in implementationType.GetConstructors()
                                          orderby constructor.GetParameters().Count() descending
                                          select constructor).Take(1);

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

            // fall back to the container's default behavior.
            return(_originalBehavior.GetConstructor(serviceType, implementationType));
        }
Beispiel #4
0
 public ConstructorInfo GetConstructor(Type serviceType,
                                       Type implementationType)
 {
     if (serviceType.IsGenericType &&
         serviceType.GetGenericTypeDefinition() == typeof(IOpenType <>))
     {
         // do alternative constructor selection here for open types.
         // For instance:
         return((
                    from ctor in implementationType.GetConstructors()
                    orderby ctor.GetParameters().Length descending
                    select ctor)
                .First());
     }
     // fall back to default behavior
     return(original.GetConstructor(serviceType, implementationType));
 }