Beispiel #1
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.IsGenericType)
            {
                Type gtd      = component.GetGenericTypeDefinition();
                Type argument = component.GetGenericArguments()[0];

                if (gtd.IsInterface && typeof(IEnumerable <>).IsAssignableFrom(gtd))
                {
                    return(GetAll(argument).CastSlow(argument));
                }
            }

            Type implementation = _mappings[component].FirstOrDefault();

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

            return(ResolveInstance(component, implementation));
        }
Beispiel #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)
        {
            if (component == typeof(IKernelConfiguration))
            {
                return(KernelConfiguration);
            }

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

                var info = gtd.GetTypeInfo();
                if (info.IsInterface && typeof(IEnumerable <>).GetTypeInfo().IsAssignableFrom(info))
                {
                    return(GetAll(argument).CastSlow(argument));
                }
            }
            Type implementation = _mappings[component].FirstOrDefault();

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

            return(ResolveInstance(component, implementation));
        }
Beispiel #3
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(IKernelConfiguration))
            {
                return(this.KernelConfiguration);
            }

            if (component == typeof(INinjectSettings))
            {
                return(this.settings);
            }

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

                if (gtd.IsInterface && typeof(IEnumerable <>).IsAssignableFrom(gtd))
                {
                    return(this.GetAll(argument).CastSlow(argument));
                }
            }

            var implementation = this.mappings[component].FirstOrDefault();

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

            return(this.ResolveInstance(component, implementation));
        }
Beispiel #4
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));
        }