Open sandboxFocus

Method IntroduceField

IntroduceField(IAdviser<INamedType>, string, IntroductionScope, OverrideStrategy, Action<IFieldBuilder>?, object?)

Introduces a field to the target type by specifying a template. Use the With<TNewDeclaration>(TNewDeclaration) method to apply the advice to a different type than the current one.

Declaration
public static IIntroductionAdviceResult<IField> IntroduceField(this IAdviser<INamedType> adviser, string template, IntroductionScope scope = IntroductionScope.Default, OverrideStrategy whenExists = OverrideStrategy.Default, Action<IFieldBuilder>? buildField = null, object? tags = null)
Parameters
Type Name Description
IAdviser<INamedType> adviser

An adviser for a named type.

string template

Name of the introduced field.

IntroductionScope scope

Determines the scope (e.g. Instance or Static) of the introduced field. The default scope is Instance.

OverrideStrategy whenExists

Determines the implementation strategy when a field of the same name is already declared in the target type. The default strategy is to fail with a compile-time error.

Action<IFieldBuilder> buildField

An optional delegate that modifies the IFieldBuilder representing the introduced field.

object tags

An optional object (typically of anonymous type) passed to the template and accessible via meta.Tags. See Sharing state with adviceSharing state with advice for details.

Returns
Type Description
IIntroductionAdviceResult<IField>

An IIntroductionAdviceResult<T> exposing the introduced IField.

See Also

IntroduceField(IAdviser<INamedType>, string, IType, IntroductionScope, OverrideStrategy, Action<IFieldBuilder>?, object?)

Introduces a field to the target type by specifying a field name and IType.

Declaration
public static IIntroductionAdviceResult<IField> IntroduceField(this IAdviser<INamedType> adviser, string fieldName, IType fieldType, IntroductionScope scope = IntroductionScope.Default, OverrideStrategy whenExists = OverrideStrategy.Default, Action<IFieldBuilder>? buildField = null, object? tags = null)
Parameters
Type Name Description
IAdviser<INamedType> adviser

An adviser for a named type.

string fieldName

Name of the introduced field.

IType fieldType

Type of the introduced field.

IntroductionScope scope

Determines the scope (e.g. Instance or Static) of the introduced field. The default scope is Instance.

OverrideStrategy whenExists

Determines the implementation strategy when a field of the same name is already declared in the target type. The default strategy is to fail with a compile-time error.

Action<IFieldBuilder> buildField

An optional delegate that modifies the IFieldBuilder representing the introduced field.

object tags

An optional object (typically of anonymous type) passed to the template and accessible via meta.Tags. See Sharing state with adviceSharing state with advice for details.

Returns
Type Description
IIntroductionAdviceResult<IField>

An IIntroductionAdviceResult<T> exposing the introduced IField.

Remarks

Unlike the template-based overload, this method allows you to introduce a field programmatically by specifying its name and type directly, without needing a template field in the aspect class. The field will be initialized to its type's default value unless customized via buildField.

Use With<TNewDeclaration>(TNewDeclaration) to introduce the field into a different type than the current target.

See Also

IntroduceField(IAdviser<INamedType>, string, Type, IntroductionScope, OverrideStrategy, Action<IFieldBuilder>?, object?)

Introduces a field to the target type by specifying a field name and Type.

Declaration
public static IIntroductionAdviceResult<IField> IntroduceField(this IAdviser<INamedType> adviser, string fieldName, Type fieldType, IntroductionScope scope = IntroductionScope.Default, OverrideStrategy whenExists = OverrideStrategy.Default, Action<IFieldBuilder>? buildField = null, object? tags = null)
Parameters
Type Name Description
IAdviser<INamedType> adviser

An adviser for a named type.

string fieldName

Name of the introduced field.

Type fieldType

Type of the introduced field.

IntroductionScope scope

Determines the scope (e.g. Instance or Static) of the introduced field. The default scope is Instance.

OverrideStrategy whenExists

Determines the implementation strategy when a field of the same name is already declared in the target type. The default strategy is to fail with a compile-time error.

Action<IFieldBuilder> buildField

An optional delegate that modifies the IFieldBuilder representing the introduced field.

object tags

An optional object (typically of anonymous type) passed to the template and accessible via meta.Tags. See Sharing state with adviceSharing state with advice for details.

Returns
Type Description
IIntroductionAdviceResult<IField>

An IIntroductionAdviceResult<T> exposing the introduced IField.

Remarks

This overload is identical to the IType-based overload, but accepts a Type parameter for convenience when working with reflection types. The field will be initialized to its type's default value unless customized via buildField.

Use With<TNewDeclaration>(TNewDeclaration) to introduce the field into a different type than the current target.

See Also