Beispiel #1
0
        private static Expression CreateLazyExpressionCall(IContainerContext containerContext, IServiceRegistration serviceRegistration, Type type, ConstructorInfo constructor, ResolutionContext resolutionContext)
        {
            var arguments = resolutionContext.ParameterExpressions != null
                ? new Expression[resolutionContext.ParameterExpressions.Sum(x => x.Length)]
                : new Expression[0];

            if (resolutionContext.ParameterExpressions != null)
            {
                var index = 0;
                for (var i = 0; i < resolutionContext.ParameterExpressions.Length; i++)
                {
                    for (var j = 0; j < resolutionContext.ParameterExpressions[i].Length; j++)
                    {
                        arguments[index++] = resolutionContext.ParameterExpressions[i][j].ConvertTo(typeof(object));
                    }
                }
            }



            var callExpression = DelegateCacheMethod.InvokeMethod(
                containerContext.AsConstant(),
                serviceRegistration.AsConstant(),
                resolutionContext.AsConstant(),
                type.AsConstant(),
                typeof(object).InitNewArray(arguments));

            return(constructor.MakeNew(callExpression.ConvertTo(type).AsLambda()));
        }
        private Expression[] CreatePostWorkExpressionIfAny(
            IContainerContext containerContext,
            IServiceRegistration serviceRegistration,
            ResolutionContext resolutionContext,
            Type serviceType,
            Expression instance)
        {
            var length = serviceRegistration.InjectionMethods.Length;

            if (this.containerExtensionManager.HasPostBuildExtensions)
            {
                length++;
            }

            if (serviceRegistration.RegistrationContext.Initializer != null)
            {
                length++;
            }

            var expressions = new Expression[length];

            if (length > 0)
            {
                this.CreateMethodExpressions(containerContext, serviceRegistration.InjectionMethods,
                                             serviceRegistration.RegistrationContext, resolutionContext, instance, expressions);
            }

            if (serviceRegistration.RegistrationContext.Initializer != null)
            {
                expressions[expressions.Length - (this.containerExtensionManager.HasPostBuildExtensions ? 2 : 1)] =
                    serviceRegistration.RegistrationContext.Initializer.AsConstant()
                    .CallMethod(serviceRegistration.RegistrationContext.Initializer.GetType().GetSingleMethod("Invoke"),
                                instance, resolutionContext.CurrentScopeParameter.ConvertTo(Constants.ResolverType));
            }

            if (this.containerExtensionManager.HasPostBuildExtensions)
            {
                var call = this.containerExtensionManager.AsConstant()
                           .CallMethod(Constants.BuildExtensionMethod,
                                       instance,
                                       containerContext.AsConstant(),
                                       resolutionContext.AsConstant(),
                                       serviceRegistration.AsConstant(),
                                       serviceType.AsConstant());

                expressions[expressions.Length - 1] = instance.AssignTo(call.ConvertTo(instance.Type));
            }

            return(expressions);
        }