Ejemplo n.º 1
0
        private static ConstructorInfo SelectConstructor(Type component, Type implementation)
        {
            var constructor = implementation.GetConstructors().OrderByDescending(c => c.GetParameters().Length).FirstOrDefault();

            if (constructor == null)
            {
                throw new InvalidOperationException(ExceptionFormatter.NoConstructorsAvailableForComponent(component, implementation));
            }

            return(constructor);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets one instance of the specified component.
        /// </summary>
        /// <param name="component">The component type.</param>
        /// <returns>The instance of the component.</returns>
        public object Get(Type component)
        {
            Ensure.ArgumentNotNull(component, "component");

            if (component == typeof(IKernel))
            {
                return(Kernel);
            }

            if (component.GetTypeInfo().IsGenericType)
            {
                Type gtd      = component.GetGenericTypeDefinition();
                Type argument = component.GetTypeInfo().GenericTypeArguments[0];

#if WINDOWS_PHONE
                Type discreteGenericType =
                    typeof(IEnumerable <>).MakeGenericType(argument);
                if (gtd.IsInterface && discreteGenericType.IsAssignableFrom(component))
                {
                    return(GetAll(argument).CastSlow(argument));
                }
#else
                var typeInfo = gtd.GetTypeInfo();
                if (typeInfo.IsInterface && typeof(IEnumerable <>).GetTypeInfo().IsAssignableFrom(typeInfo))
                {
                    return(GetAll(argument).CastSlow(argument));
                }
#endif
            }
            Type implementation = _mappings[component].FirstOrDefault();

            if (implementation == null)
            {
                throw new InvalidOperationException(ExceptionFormatter.NoSuchComponentRegistered(component));
            }

            return(ResolveInstance(component, implementation));
        }