Open sandboxFocus

Method IntroduceParameter

IntroduceParameter(IAdviser<IConstructor>, string, IType, TypedConstant, Func<IParameter, IConstructor, PullAction>?, ImmutableArray<AttributeConstruction>)

Appends a parameter to a constructor by specifying its name and IType. Use the With<TNewDeclaration>(TNewDeclaration) method to introduce the parameter to a different constructor than the current one.

Declaration
[Obsolete("This overload does not work across project boundaries. Use an overload that accepts an IIntroduceConstructorParameterPullStrategy.")]
public static IIntroductionAdviceResult<IParameter> IntroduceParameter(this IAdviser<IConstructor> adviser, string parameterName, IType parameterType, TypedConstant defaultValue, Func<IParameter, IConstructor, PullAction>? pullAction, ImmutableArray<AttributeConstruction> attributes = default)
Parameters
Type Name Description
IAdviser<IConstructor> adviser

An adviser for a constructor.

string parameterName

The name of the parameter.

IType parameterType

The type of the parameter.

TypedConstant defaultValue

The default value of the parameter (required). It must be type-compatible with parameterType. To specify default as the default value, use Default(IType, bool).

Func<IParameter, IConstructor, PullAction> pullAction

An optional delegate that returns a PullAction specifying how to pull the new parameter from other child constructors. A null value is equivalent to None, i.e. defaultValue of the parameter will be used.

ImmutableArray<AttributeConstruction> attributes

An optional list of custom attributes to add to the introduced parameter.

Returns
Type Description
IIntroductionAdviceResult<IParameter>

An IIntroductionAdviceResult<T> exposing the introduced IParameter.

IntroduceParameter(IAdviser<IConstructor>, string, IType, TypedConstant, IPullStrategy?, ImmutableArray<AttributeConstruction>)

Appends an optional parameter (i.e. one that has a compile-time-constant default value) to a constructor by specifying its name and IType. The new parameter is added directly to the existing constructor; existing callers can omit the argument, so source compatibility is preserved. Binary compatibility is not preserved. Because the constructor count is unchanged, this overload is the right choice for dependency-injection scenarios where the container requires exactly one applicable constructor (for example, Microsoft.Extensions.DependencyInjection.ActivatorUtilities).

Declaration
public static IIntroductionAdviceResult<IParameter> IntroduceParameter(this IAdviser<IConstructor> adviser, string parameterName, IType parameterType, TypedConstant defaultValue, IPullStrategy? pullStrategy = null, ImmutableArray<AttributeConstruction> attributes = default)
Parameters
Type Name Description
IAdviser<IConstructor> adviser

An adviser for a constructor.

string parameterName

The name of the parameter.

IType parameterType

The type of the parameter.

TypedConstant defaultValue

The default value of the parameter. It must be type-compatible with parameterType. To specify default as the default value, use Default(IType, bool).

IPullStrategy pullStrategy

An optional IPullStrategy that specifies how to pull the new parameter from chained or derived constructors.

ImmutableArray<AttributeConstruction> attributes

An optional list of custom attributes to add to the introduced parameter.

Returns
Type Description
IIntroductionAdviceResult<IParameter>

An IIntroductionAdviceResult<T> exposing the introduced IParameter.

See Also

IntroduceParameter(IAdviser<IConstructor>, string, IType, IPullStrategy?, ImmutableArray<AttributeConstruction>, IConstructorOverloadingStrategy?)

Appends a required parameter (i.e. one that has no default value) to a constructor by specifying its name and IType. The framework also emits a forwarding constructor: a compile-time stub with the pre-mutation signature that chains via : this(...) to the now-mutated constructor. The forwarding constructor obtains the new parameter's value from the supplied IPullStrategy (for example, UseExpression(IExpression) for a non-constant expression such as DateTime.Now, or IntroduceParameterAndPull(string?, IType?, IExpression?, IExpression?, bool, bool) to cascade a dependency into chained and derived constructors). Because the pre-mutation signature remains callable, both source and binary compatibility are preserved. This overload is the right choice when binary compatibility matters, when the default must be a non-constant expression, or when a parameterless constructor must remain callable (for example, through CreateInstance<T>() or the new() generic constraint). Pass a custom IConstructorOverloadingStrategy to scope which constructors receive a forwarding constructor, or to opt out entirely.

Declaration
public static IIntroductionAdviceResult<IParameter> IntroduceParameter(this IAdviser<IConstructor> adviser, string parameterName, IType parameterType, IPullStrategy? pullStrategy = null, ImmutableArray<AttributeConstruction> attributes = default, IConstructorOverloadingStrategy? overloadingStrategy = null)
Parameters
Type Name Description
IAdviser<IConstructor> adviser

An adviser for a constructor.

string parameterName

The name of the parameter.

IType parameterType

The type of the parameter.

IPullStrategy pullStrategy

An optional IPullStrategy that specifies how to supply the new parameter's value in the forwarding constructor and in chained or derived constructors.

ImmutableArray<AttributeConstruction> attributes

An optional list of custom attributes to add to the introduced parameter.

IConstructorOverloadingStrategy overloadingStrategy

An optional IConstructorOverloadingStrategy that decides, per mutated constructor, whether the framework should generate a forwarding constructor preserving the pre-mutation signature. A null value (the default) is interpreted as ForwardSourceConstructors, which is the safe default. The value passed for the introduced parameter inside the generated forwarder is controlled by IntroduceParameterAndPull(string?, IType?, IExpression?, IExpression?, bool, bool)'s forwarderExpression argument.

Returns
Type Description
IIntroductionAdviceResult<IParameter>

An IIntroductionAdviceResult<T> exposing the introduced IParameter.

See Also

IntroduceParameter(IAdviser<IConstructor>, string, Type, TypedConstant, Func<IParameter, IConstructor, PullAction>?, ImmutableArray<AttributeConstruction>)

Appends a parameter to a constructor by specifying its name and Type. Use the With<TNewDeclaration>(TNewDeclaration) method to apply the advice to another constructor than the current one.

Declaration
[Obsolete("This overload does not work across project boundaries. Use an overload that accepts an IIntroduceConstructorParameterPullStrategy.")]
public static IIntroductionAdviceResult<IParameter> IntroduceParameter(this IAdviser<IConstructor> adviser, string parameterName, Type parameterType, TypedConstant defaultValue, Func<IParameter, IConstructor, PullAction>? pullAction, ImmutableArray<AttributeConstruction> attributes = default)
Parameters
Type Name Description
IAdviser<IConstructor> adviser

An adviser for a constructor.

string parameterName

The name of the parameter.

Type parameterType

The type of the parameter.

TypedConstant defaultValue

The default value of the parameter (required). It must be type-compatible with parameterType. To specify default as the default value, use Default(IType, bool).

Func<IParameter, IConstructor, PullAction> pullAction

An optional delegate that returns a PullAction specifying how to pull the new parameter from other child constructors. A null value is equivalent to None, i.e. defaultValue of the parameter will be used.

ImmutableArray<AttributeConstruction> attributes

An optional list of custom attributes to add to the introduced parameter.

Returns
Type Description
IIntroductionAdviceResult<IParameter>

An IIntroductionAdviceResult<T> exposing the introduced IParameter.

IntroduceParameter(IAdviser<IConstructor>, string, Type, TypedConstant, IPullStrategy?, ImmutableArray<AttributeConstruction>)

Appends an optional parameter (i.e. one that has a compile-time-constant default value) to a constructor by specifying its name and Type. The new parameter is added directly to the existing constructor; existing callers can omit the argument, so source compatibility is preserved. Binary compatibility is not preserved. Because the constructor count is unchanged, this overload is the right choice for dependency-injection scenarios where the container requires exactly one applicable constructor (for example, Microsoft.Extensions.DependencyInjection.ActivatorUtilities).

Declaration
public static IIntroductionAdviceResult<IParameter> IntroduceParameter(this IAdviser<IConstructor> adviser, string parameterName, Type parameterType, TypedConstant defaultValue, IPullStrategy? pullStrategy = null, ImmutableArray<AttributeConstruction> attributes = default)
Parameters
Type Name Description
IAdviser<IConstructor> adviser

An adviser for a constructor.

string parameterName

The name of the parameter.

Type parameterType

The type of the parameter.

TypedConstant defaultValue

The default value of the parameter. It must be type-compatible with parameterType. To specify default as the default value, use Default(IType, bool).

IPullStrategy pullStrategy

An optional IPullStrategy that specifies how to pull the new parameter from chained or derived constructors.

ImmutableArray<AttributeConstruction> attributes

An optional list of custom attributes to add to the introduced parameter.

Returns
Type Description
IIntroductionAdviceResult<IParameter>

An IIntroductionAdviceResult<T> exposing the introduced IParameter.

See Also

IntroduceParameter(IAdviser<IConstructor>, string, Type, IPullStrategy?, ImmutableArray<AttributeConstruction>, IConstructorOverloadingStrategy?)

Appends a required parameter (i.e. one that has no default value) to a constructor by specifying its name and Type. The framework also emits a forwarding constructor: a compile-time stub with the pre-mutation signature that chains via : this(...) to the now-mutated constructor. The forwarding constructor obtains the new parameter's value from the supplied IPullStrategy (for example, UseExpression(IExpression) for a non-constant expression such as DateTime.Now, or IntroduceParameterAndPull(string?, IType?, IExpression?, IExpression?, bool, bool) to cascade a dependency into chained and derived constructors). Because the pre-mutation signature remains callable, both source and binary compatibility are preserved. This overload is the right choice when binary compatibility matters, when the default must be a non-constant expression, or when a parameterless constructor must remain callable (for example, through CreateInstance<T>() or the new() generic constraint). Pass a custom IConstructorOverloadingStrategy to scope which constructors receive a forwarding constructor, or to opt out entirely.

Declaration
public static IIntroductionAdviceResult<IParameter> IntroduceParameter(this IAdviser<IConstructor> adviser, string parameterName, Type parameterType, IPullStrategy? pullStrategy = null, ImmutableArray<AttributeConstruction> attributes = default, IConstructorOverloadingStrategy? overloadingStrategy = null)
Parameters
Type Name Description
IAdviser<IConstructor> adviser

An adviser for a constructor.

string parameterName

The name of the parameter.

Type parameterType

The type of the parameter.

IPullStrategy pullStrategy

An optional IPullStrategy that specifies how to supply the new parameter's value in the forwarding constructor and in chained or derived constructors.

ImmutableArray<AttributeConstruction> attributes

An optional list of custom attributes to add to the introduced parameter.

IConstructorOverloadingStrategy overloadingStrategy

An optional IConstructorOverloadingStrategy that decides, per mutated constructor, whether the framework should generate a forwarding constructor preserving the pre-mutation signature. A null value (the default) is interpreted as ForwardSourceConstructors, which is the safe default.

Returns
Type Description
IIntroductionAdviceResult<IParameter>

An IIntroductionAdviceResult<T> exposing the introduced IParameter.

See Also