Open sandboxFocus

Method AddInitializer

AddInitializer(IAdviser<INamedType>, string, InitializerKind, object?, object?)

Adds a type or instance initializer by using a template. Use the With<TNewDeclaration>(TNewDeclaration) method to add the initializer to a different type than the current one.

Declaration
public static IAddInitializerAdviceResult AddInitializer(this IAdviser<INamedType> adviser, string template, InitializerKind kind, object? args = null, object? tags = null)
Parameters
Type Name Description
IAdviser<INamedType> adviser

An adviser for a named type.

string template

The name of the template. This method must have no run-time parameter, be of void return type, and be annotated with TemplateAttribute.

InitializerKind kind

The type of initializer to add.

object args

An object (typically of anonymous type) whose properties map to parameters or type parameters of the template.

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
IAddInitializerAdviceResult

An IAddInitializerAdviceResult exposing the added initializer.

Remarks

This is the programmatic way to add initializers from BuildAspect(IAspectBuilder<T>). Initializers inject code before any user constructor code runs (for BeforeInstanceConstructor) or at the beginning of the static constructor (for BeforeTypeConstructor).

For BeforeInstanceConstructor, the initializer will NOT affect constructors that chain to another constructor using : this(...). It always runs before any constructor of the current class, but after the call to the : base(...) constructor.

A default constructor will be created automatically if the type does not contain any constructor and BeforeInstanceConstructor is used.

See Also

AddInitializer(IAdviser<INamedType>, IStatement, InitializerKind)

Adds a type or instance initializer by specifying an IStatement. Use the With<TNewDeclaration>(TNewDeclaration) method to add the initializer to a different type than the current one.

Declaration
public static IAddInitializerAdviceResult AddInitializer(this IAdviser<INamedType> adviser, IStatement statement, InitializerKind kind)
Parameters
Type Name Description
IAdviser<INamedType> adviser

An adviser for a named type.

IStatement statement

The statement to be inserted at the top of constructors.

InitializerKind kind

The type of initializer to add.

Returns
Type Description
IAddInitializerAdviceResult

An IAddInitializerAdviceResult exposing the added initializer.

Remarks

This overload allows you to inject a programmatically constructed statement instead of using a template. See AddInitializer(IAdviser<INamedType>, string, InitializerKind, object?, object?) for more details on initialization timing.

See Also

AddInitializer(IAdviser<IConstructor>, string, object?, object?)

Adds an initializer to a specific constructor by using a template. Use the With<TNewDeclaration>(TNewDeclaration) method to add the initializer to a different constructor than the current one.

Declaration
public static IAddInitializerAdviceResult AddInitializer(this IAdviser<IConstructor> adviser, string template, object? args = null, object? tags = null)
Parameters
Type Name Description
IAdviser<IConstructor> adviser

An adviser for a constructor.

string template

The name of the template. This method must have no run-time parameter, be of void return type, and be annotated with TemplateAttribute.

object args

An object (typically of anonymous type) whose properties map to parameters or type parameters of the template.

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
IAddInitializerAdviceResult

An IAddInitializerAdviceResult exposing the added initializer.

Remarks

This overload allows you to target a specific constructor, including constructors that chain to another constructor using : this(...). The initialization code runs at the beginning of the constructor, after the : base(...) or : this(...) call.

See Also

AddInitializer(IAdviser<IConstructor>, IStatement)

Adds an initializer to a specific constructor by specifying an IStatement. Use the With<TNewDeclaration>(TNewDeclaration) method to add the initializer to a different constructor than the current one.

Declaration
public static IAddInitializerAdviceResult AddInitializer(this IAdviser<IConstructor> adviser, IStatement statement)
Parameters
Type Name Description
IAdviser<IConstructor> adviser

An adviser for a constructor.

IStatement statement

The statement to be inserted at the top of the constructor.

Returns
Type Description
IAddInitializerAdviceResult

An IAddInitializerAdviceResult exposing the added initializer.

Remarks

This overload allows you to inject a programmatically constructed statement to a specific constructor. See AddInitializer(IAdviser<IConstructor>, string, object?, object?) for initialization timing details.

See Also