Пример #1
0
        public Expression GetExpression(IContainerContext containerContext, IServiceRegistration serviceRegistration, ResolutionContext resolutionContext, Type resolveType)
        {
            if (serviceRegistration.IsDecorator || resolutionContext.IsCurrentlyDecorating(resolveType))
            {
                return(this.BuildDisposalTrackingAndFinalizerExpression(containerContext, serviceRegistration, resolutionContext, resolveType));
            }

            var decorators = containerContext.DecoratorRepository.GetDecoratorsOrDefault(resolveType);

            if (decorators == null)
            {
                if (!resolveType.IsClosedGenericType())
                {
                    return(this.BuildDisposalTrackingAndFinalizerExpression(containerContext, serviceRegistration, resolutionContext, resolveType));
                }

                decorators = containerContext.DecoratorRepository.GetDecoratorsOrDefault(resolveType.GetGenericTypeDefinition());
                if (decorators == null)
                {
                    return(this.BuildDisposalTrackingAndFinalizerExpression(containerContext, serviceRegistration, resolutionContext, resolveType));
                }
            }

            resolutionContext.AddCurrentlyDecoratingType(resolveType);
            var expression = this.BuildDisposalTrackingAndFinalizerExpression(containerContext, serviceRegistration, resolutionContext, resolveType);

            if (expression == null)
            {
                return(null);
            }

            var length = decorators.Length;

            for (int i = 0; i < length; i++)
            {
                var decorator = decorators[i];
                resolutionContext.SetExpressionOverride(resolveType, expression);
                expression = decorator.Value.GetExpression(containerContext, resolutionContext, resolveType);
                if (expression == null)
                {
                    return(null);
                }
            }

            resolutionContext.ClearCurrentlyDecoratingType(resolveType);
            return(expression);
        }