Exemple #1
0
            private Type GetImplementationTypeThroughFactory(Type serviceType, InjectionConsumerInfo consumer)
            {
                Type implementationType =
                    this.ImplementationTypeFactory(new TypeFactoryContext(serviceType, consumer));

                if (implementationType == null)
                {
                    throw new InvalidOperationException(StringResources.FactoryReturnedNull(this.ServiceType));
                }

                if (implementationType.ContainsGenericParameters())
                {
                    Requires.TypeFactoryReturnedTypeThatDoesNotContainUnresolvableTypeArguments(
                        serviceType, implementationType);

                    // implementationType == null when type constraints don't match.
                    implementationType =
                        GenericTypeBuilder.MakeClosedImplementation(serviceType, implementationType);
                }
                else
                {
                    Requires.FactoryReturnsATypeThatIsAssignableFromServiceType(serviceType, implementationType);
                }

                return(implementationType);
            }
Exemple #2
0
            public InstanceProducer TryGetProducer(Type serviceType, InjectionConsumerInfo consumer,
                                                   bool handled)
            {
                Type closedImplementation = this.ImplementationType != null
                    ? GenericTypeBuilder.MakeClosedImplementation(serviceType, this.ImplementationType)
                    : null;

                Func <Type> implementationTypeProvider = () => this.ImplementationType != null
                    ? closedImplementation
                    : this.GetImplementationTypeThroughFactory(serviceType, consumer);

                var context = new PredicateContext(serviceType, implementationTypeProvider, consumer, handled);

                // NOTE: The producer should only get built after it matches the delegate, to prevent
                // unneeded producers from being created, because this might cause diagnostic warnings,
                // such as torn lifestyle warnings.
                var shouldBuildProducer =
                    (this.ImplementationType == null || closedImplementation != null) &&
                    this.MatchesPredicate(context) &&
                    context.ImplementationType != null;

                return(shouldBuildProducer
                    ? this.GetProducer(serviceType, context.ImplementationType)
                    : null);
            }
                public InstanceProducer TryGetProducer(Type serviceType, InjectionConsumerInfo consumer,
                                                       bool handled)
                {
                    Type closedImplementation =
                        GenericTypeBuilder.MakeClosedImplementation(serviceType, this.ImplementationType);

                    var context = new PredicateContext(serviceType, closedImplementation, consumer, handled);

                    // NOTE: The producer should only get built after it matches the delegate, to prevent
                    // unneeded producers from being created, because this might cause diagnostic warnings,
                    // such as torn lifestyle warnings.
                    return(closedImplementation != null && this.MatchesPredicate(context)
                        ? this.GetProducer(serviceType, closedImplementation)
                        : null);
                }
                private InstanceProducer GetOrBuildProducerFromCache(Type serviceType)
                {
                    InstanceProducer producer;

                    lock (this.cache)
                    {
                        if (this.cache.TryGetValue(serviceType, out producer))
                        {
                            return(producer);
                        }

                        Type closedImplementation =
                            GenericTypeBuilder.MakeClosedImplementation(serviceType, this.ImplementationType);

                        if (closedImplementation != null)
                        {
                            producer = this.CreateNewProducerFor(serviceType, closedImplementation);
                        }

                        this.cache[serviceType] = producer;
                    }

                    return(producer);
                }
Exemple #5
0
 public bool MatchesServiceType(Type serviceType) =>
 GenericTypeBuilder.MakeClosedImplementation(serviceType, this.ImplementationType) != null;