Open sandboxFocus

Method IntroduceParameterAndPull

IntroduceParameterAndPull(string, IType, IExpression?, IExpression?, ImmutableArray<AttributeConstruction>, bool)

Creates a PullAction that means that the dependency should be pulled from a new parameter of the calling constructor.

Declaration
public static PullAction IntroduceParameterAndPull(string parameterName, IType parameterType, IExpression? parameterDefaultValue, IExpression? forwarderExpression = null, ImmutableArray<AttributeConstruction> parameterAttributes = default, bool materializeOnRecord = false)
Parameters
Type Name Description
string parameterName

Name of the new parameter.

IType parameterType

Type of the new parameter.

IExpression parameterDefaultValue

Optional default value for the new parameter. Must be a compile-time constant (or null), because it becomes the declared default of a parameter in the chained constructor.

IExpression forwarderExpression

Optional expression to pass for the introduced parameter in the framework-emitted forwarding constructor (the stub that preserves the pre-mutation signature when the required-parameter overload is used). Unlike parameterDefaultValue, this expression does not need to be a compile-time constant (e.g. System.DateTime.Now is valid). When a forwarder is emitted and this argument is null, parameterDefaultValue is used if non-null; otherwise the framework falls back to default(T)! so the forwarder always compiles.

ImmutableArray<AttributeConstruction> parameterAttributes

Optional attributes to apply to the new parameter.

bool materializeOnRecord

When true and the target is a record, the parameter is appended to the positional (primary) constructor and therefore becomes part of the record's value shape: an auto-generated property, a new entry in Equals/GetHashCode/ToString, and a new Deconstruct overload. When false (the default), the parameter is carried on a non-primary sibling constructor synthesized by Metalama, and the record's positional list is left untouched. Ignored for non-record targets. Set this to true only if you deliberately want the pulled parameter to participate in the record's value identity.

Returns
Type Description
PullAction

A PullAction that introduces a new parameter and pulls from it.

Remarks

This action propagates the parameter requirement to child constructors. A new parameter with the specified name, type, and default value will be added to each child constructor, and its value will be passed to the base/this constructor.

Cross-Project Support: This pull action operates across project boundaries. When a parameter is introduced to a base class in one project, all derived classes in referencing projects will automatically have the parameter added to their constructors. This makes it ideal for framework-level dependency injection.

This is the most common pull action for dependency injection scenarios where derived classes should also receive and forward the dependency.