Ejemplo n.º 1
0
        public IServiceCallSite CreateCallSite(ServiceProvider provider, ISet <Type> callSiteChain)
        {
            ConstructorInfo[] constructors = _descriptor.ImplementationType.GetTypeInfo()
                                             .DeclaredConstructors
                                             .Where(IsInjectable)
                                             .ToArray();

            // TODO: actual service-fulfillment constructor selection
            if (constructors.Length == 1)
            {
                ParameterInfo[]    parameters         = constructors[0].GetParameters();
                IServiceCallSite[] parameterCallSites = new IServiceCallSite[parameters.Length];
                for (var index = 0; index != parameters.Length; ++index)
                {
                    parameterCallSites[index] = provider.GetServiceCallSite(parameters[index].ParameterType, callSiteChain);

                    if (parameterCallSites[index] == null && parameters[index].HasDefaultValue)
                    {
                        parameterCallSites[index] = new ConstantCallSite(parameters[index].DefaultValue);
                    }
                    if (parameterCallSites[index] == null)
                    {
                        throw new InvalidOperationException(Resources.FormatCannotResolveService(
                                                                parameters[index].ParameterType,
                                                                _descriptor.ImplementationType));
                    }
                }
                return(new ConstructorCallSite(constructors[0], parameterCallSites));
            }

            return(new CreateInstanceCallSite(_descriptor));
        }
Ejemplo n.º 2
0
        public IServiceCallSite CreateCallSite(ServiceProvider provider)
        {
            ConstructorInfo[] constructors = _descriptor.ImplementationType.GetTypeInfo()
                                             .DeclaredConstructors
                                             .Where(IsInjectable)
                                             .ToArray();

            // TODO: actual service-fulfillment constructor selection
            if (constructors.Length == 1)
            {
                ParameterInfo[]    parameters         = constructors[0].GetParameters();
                IServiceCallSite[] parameterCallSites = new IServiceCallSite[parameters.Length];
                for (var index = 0; index != parameters.Length; ++index)
                {
                    parameterCallSites[index] = provider.GetServiceCallSite(parameters[index].ParameterType);
                    if (parameterCallSites[index] == null && parameters[index].HasDefaultValue)
                    {
                        parameterCallSites[index] = new ConstantCallSite(parameters[index].DefaultValue);
                    }
                    if (parameterCallSites[index] == null)
                    {
                        throw new Exception(
                                  string.Format("TODO: Unable to resolve service for type '{0}' while attempting to activate '{1}'.",
                                                parameters[index].ParameterType, _descriptor.ImplementationType));
                    }
                }
                return(new ConstructorCallSite(constructors[0], parameterCallSites));
            }

            return(new CreateInstanceCallSite(_descriptor));
        }
Ejemplo n.º 3
0
        private IServiceCallSite[] PopulateCallSites(
            ServiceProvider provider,
            ISet <Type> callSiteChain,
            ParameterInfo[] parameters,
            bool throwIfCallSiteNotFound)
        {
            var parameterCallSites = new IServiceCallSite[parameters.Length];

            for (var index = 0; index < parameters.Length; index++)
            {
                var callSite = provider.GetServiceCallSite(parameters[index].ParameterType, callSiteChain);

                if (callSite == null && parameters[index].HasDefaultValue)
                {
                    callSite = new ConstantCallSite(parameters[index].DefaultValue);
                }

                if (callSite == null)
                {
                    if (throwIfCallSiteNotFound)
                    {
                        throw new InvalidOperationException(Resources.FormatCannotResolveService(
                                                                parameters[index].ParameterType,
                                                                _descriptor.ImplementationType));
                    }

                    return(null);
                }

                parameterCallSites[index] = callSite;
            }

            return(parameterCallSites);
        }
Ejemplo n.º 4
0
        public IServiceCallSite CreateCallSite(ServiceProvider provider, ISet<Type> callSiteChain)
        {
            ConstructorInfo[] constructors = _descriptor.ImplementationType.GetTypeInfo()
                .DeclaredConstructors
                .Where(IsInjectable)
                .ToArray();

            // TODO: actual service-fulfillment constructor selection
            if (constructors.Length == 1)
            {
                ParameterInfo[] parameters = constructors[0].GetParameters();
                IServiceCallSite[] parameterCallSites = new IServiceCallSite[parameters.Length];
                for (var index = 0; index != parameters.Length; ++index)
                {
                    parameterCallSites[index] = provider.GetServiceCallSite(parameters[index].ParameterType, callSiteChain);

                    if (parameterCallSites[index] == null && parameters[index].HasDefaultValue)
                    {
                        parameterCallSites[index] = new ConstantCallSite(parameters[index].DefaultValue);
                    }
                    if (parameterCallSites[index] == null)
                    {
                        throw new InvalidOperationException(Resources.FormatCannotResolveService(
                                parameters[index].ParameterType,
                                _descriptor.ImplementationType));
                    }
                }
                return new ConstructorCallSite(constructors[0], parameterCallSites);
            }

            return new CreateInstanceCallSite(_descriptor);
        }
Ejemplo n.º 5
0
        private IServiceCallSite[] PopulateCallSites(
            ServiceProvider provider,
            ISet<Type> callSiteChain,
            ParameterInfo[] parameters,
            bool throwIfCallSiteNotFound)
        {
            var parameterCallSites = new IServiceCallSite[parameters.Length];
            for (var index = 0; index < parameters.Length; index++)
            {
                var callSite = provider.GetServiceCallSite(parameters[index].ParameterType, callSiteChain);

                if (callSite == null && parameters[index].HasDefaultValue)
                {
                    callSite = new ConstantCallSite(parameters[index].DefaultValue);
                }

                if (callSite == null)
                {
                    if (throwIfCallSiteNotFound)
                    {
                        throw new InvalidOperationException(Resources.FormatCannotResolveService(
                            parameters[index].ParameterType,
                            _descriptor.ImplementationType));
                    }

                    return null;
                }

                parameterCallSites[index] = callSite;
            }

            return parameterCallSites;
        }