/// <summary> /// Called during the chain of responsibility for a build operation. /// </summary> /// <remarks>Existing object is an instance of <see cref="DynamicBuildPlanGenerationContext"/>.</remarks> /// <param name="context">The context for the operation.</param> public override void PreBuildUp(IBuilderContext context) { DynamicBuildPlanGenerationContext buildContext = (DynamicBuildPlanGenerationContext)(context ?? throw new ArgumentNullException(nameof(context))).Existing; GuardTypeIsNonPrimitive(context); buildContext.AddToBuildPlan( Expression.IfThen( Expression.Equal( buildContext.GetExistingObjectExpression(), Expression.Constant(null)), CreateInstanceBuildupExpression(buildContext, context))); buildContext.AddToBuildPlan( Expression.Call(null, SetPerBuildSingletonMethod, buildContext.ContextParameter)); }
private IEnumerable <Expression> CreateNewBuildupSequence(DynamicBuildPlanGenerationContext buildContext, SelectedConstructor selectedConstructor, string signature) { var parameterExpressions = BuildConstructionParameterExpressions(buildContext, selectedConstructor, signature); var newItemExpression = Expression.Variable(selectedConstructor.Constructor.DeclaringType, "newItem"); yield return(Expression.Call(null, SetCurrentOperationToInvokingConstructorMethod, Expression.Constant(signature), buildContext.ContextParameter)); yield return(Expression.Assign( buildContext.GetExistingObjectExpression(), Expression.Convert( Expression.New(selectedConstructor.Constructor, parameterExpressions), typeof(object)))); yield return(buildContext.GetClearCurrentOperationExpression()); }