コード例 #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);
            }
コード例 #2
0
        internal static Type MakeClosedImplementation(Type closedAbstraction, Type openImplementation)
        {
            var builder = new GenericTypeBuilder(closedAbstraction, openImplementation);
            var results = builder.BuildClosedGenericImplementation();

            return(results.ClosedGenericImplementation);
        }
コード例 #3
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);
            }
コード例 #4
0
                public bool OverlapsWith(Type serviceType)
                {
                    if (this.Predicate != null)
                    {
                        // Conditionals never overlap compile time.
                        return(false);
                    }

                    return(GenericTypeBuilder.IsImplementationApplicableToEveryGenericType(serviceType,
                                                                                           this.ImplementationType));
                }
コード例 #5
0
                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);
                }
コード例 #6
0
                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);
                }
コード例 #7
0
 private bool IsImplementationApplicableToEveryGenericType()
 {
     return(GenericTypeBuilder.IsImplementationApplicableToEveryGenericType(
                this.ServiceType,
                this.ImplementationType));
 }
コード例 #8
0
 internal static Type MakeClosedImplementation(Type closedAbstraction, Type openImplementation)
 {
     var builder = new GenericTypeBuilder(closedAbstraction, openImplementation);
     var results = builder.BuildClosedGenericImplementation();
     return results.ClosedGenericImplementation;
 }
コード例 #9
0
        private GenericTypeBuilder.BuildResult BuildClosedGenericImplementation(Type serviceType)
        {
            var builder = new GenericTypeBuilder(serviceType, this.DecoratorTypeDefinition);

            return builder.BuildClosedGenericImplementation();
        }
コード例 #10
0
        private Type GetDecoratorTypeFromDecoratorFactory(Type requestedServiceType, 
            DecoratorPredicateContext context)
        {
            Type decoratorType = this.data.DecoratorTypeFactory(context);

            if (decoratorType.ContainsGenericParameters)
            {
                if (!requestedServiceType.IsGenericType)
                {
                    throw new ActivationException(
                        StringResources.TheDecoratorReturnedFromTheFactoryShouldNotBeOpenGeneric(
                            requestedServiceType, decoratorType));
                }

                Requires.TypeFactoryReturnedTypeThatDoesNotContainUnresolvableTypeArguments(
                    requestedServiceType, decoratorType);

                var builder = new GenericTypeBuilder(requestedServiceType, decoratorType);

                decoratorType = builder.BuildClosedGenericImplementation().ClosedGenericImplementation;

                // decoratorType == null when type constraints don't match.
                if (decoratorType != null)
                {
                    Requires.HasFactoryCreatedDecorator(this.data.Container, requestedServiceType, decoratorType);
                }
            }
            else
            {
                Requires.DecoratorFactoryReturnsATypeThatIsAssignableFromServiceType(decoratorType, requestedServiceType);
            }

            return decoratorType;
        }
コード例 #11
0
 public bool MatchesServiceType(Type serviceType) =>
 GenericTypeBuilder.MakeClosedImplementation(serviceType, this.ImplementationType) != null;
コード例 #12
0
 public bool OverlapsWith(InstanceProducer producerToCheck) =>
 this.IsConditional || this.ImplementationType == null
         ? false // Conditionals never overlap compile time.
         : GenericTypeBuilder.IsImplementationApplicableToEveryGenericType(
     producerToCheck.ServiceType,
     this.ImplementationType);
コード例 #13
0
 public bool OverlapsWith(Type serviceType) =>
 this.Predicate != null || this.ImplementationType == null
         ? false // Conditionals never overlap compile time.
         : GenericTypeBuilder.IsImplementationApplicableToEveryGenericType(serviceType,
                                                                           this.ImplementationType);