Open sandboxFocus

Method IntroduceParameterAndPull

IntroduceParameterAndPull(string?, IType?, IExpression?, IExpression?, bool, bool)

Creates a pull strategy that introduces a new parameter in the child constructor and passes its value to the introduced parameter.

Declaration
public static IPullStrategy IntroduceParameterAndPull(string? name = null, IType? type = null, IExpression? defaultValue = null, IExpression? forwarderExpression = null, bool reuseExistingParameterOfCompatibleType = false, bool materializeOnRecord = false)
Parameters
Type Name Description
string name

The name for the new parameter in the child constructor. If null, the introduced parameter's name is used.

IType type

The type for the new parameter in the child constructor. If null, the introduced parameter's type is used.

IExpression defaultValue

The default value for the new parameter in the child constructor. If null, no default value is specified. Must be a compile-time constant expression (or null).

IExpression forwarderExpression

Expression passed for the introduced parameter inside the framework-emitted forwarding constructor (the stub that preserves the pre-mutation signature when the required-parameter overload is used). Unlike defaultValue, this expression does not need to be a compile-time constant. When a forwarder is emitted and this argument is null, defaultValue is used if non-null; otherwise the framework falls back to default(T)!.

bool reuseExistingParameterOfCompatibleType

When true, if a child constructor already has a parameter whose type is convertible to the introduced parameter's type (i.e. equally or more specific), that existing parameter is forwarded to the base constructor instead of introducing a duplicate. If an existing introduced (aspect-generated) parameter has a less-specific type (e.g. ILogger<Base> when ILogger<Derived> is needed), its type is automatically replaced with the more specific type. Source-defined parameters are never modified; when a source-defined parameter has the same name but an incompatible type, a new parameter with a deduplicated name is introduced instead. The default is false: a new parameter is always introduced.

bool materializeOnRecord

When true and the target is a record, the introduced parameter is appended to the positional (primary) constructor and becomes part of the record's value shape (property, Deconstruct, Equals, ToString). 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.

Returns
Type Description
IPullStrategy

A pull strategy that introduces a new parameter in child constructors.

Remarks

This strategy propagates the parameter requirement down the inheritance hierarchy. When a parameter is introduced in a base constructor using this strategy, each child constructor will also receive a new parameter (with the specified name, type, and default value), and that parameter's value will be passed to the base constructor.

This pull strategy operates across project boundaries. Changes are applied to all types derived from the modified type, even if those derived types are defined in different projects that reference the project containing the base type.

This is the most common pull strategy when you want derived classes to provide values for the introduced parameter, especially for dependency injection scenarios where dependencies should flow through the constructor hierarchy.

Example: If you introduce an ILogger parameter to a base class constructor with this strategy, all derived class constructors will also get an ILogger parameter added, and they will pass it to the base constructor.

See Also