Open sandboxFocus

Method IntroduceMethod

IntroduceMethod(IAdviser<INamedType>, string, IntroductionScope, OverrideStrategy, Action<IMethodBuilder>?, object?, object?)

Introduces a new method to a type, or optionally overrides an existing method with the same signature.

Declaration
public static IIntroductionAdviceResult<IMethod> IntroduceMethod(this IAdviser<INamedType> adviser, string template, IntroductionScope scope = IntroductionScope.Default, OverrideStrategy whenExists = OverrideStrategy.Default, Action<IMethodBuilder>? buildMethod = null, object? args = null, object? tags = null)
Parameters
Type Name Description
IAdviser<INamedType> adviser

An adviser for a named type.

string template

Name of the template method in the aspect class. This method must be annotated with TemplateAttribute. The template method can have parameters and a return type, which can be further customized using buildMethod.

IntroductionScope scope

Determines whether the introduced method is instance or static. Default behavior: if the template is static, the introduced method is static; otherwise, it matches the target declaration's scope.

OverrideStrategy whenExists

Strategy when a method with the same name and signature already exists in the target type. Default is Fail (compile-time error). Use Override to replace the existing method, or Ignore to skip introduction silently.

Action<IMethodBuilder> buildMethod

Optional delegate to customize the introduced method's characteristics (name, accessibility, modifiers, parameters, etc.) via IMethodBuilder.

object args

An optional object (typically of anonymous type) whose properties map to template method parameters or type parameters. See Sharing state with adviceSharing state with advice for details.

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<IMethod>

An IIntroductionAdviceResult<T> providing access to the introduced method declaration.

Remarks

This is the imperative (programmatic) way to introduce methods from BuildAspect(IAspectBuilder<T>). For a declarative approach, use IntroduceAttribute(IAdviser<IDeclaration>, IAttributeData, OverrideStrategy) on a template method in the aspect class.

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

See Also