Open sandboxFocus

Method IntroduceEvent

IntroduceEvent(IAdviser<INamedType>, string, IntroductionScope, OverrideStrategy, Action<IEventBuilder>?, object?)

Introduces a new event to the target type, or overrides the implementation of an existing one, by specifying an event template. Use the With<TNewDeclaration>(TNewDeclaration) method to introduce the event to a different type than the current one.

Declaration
public static IIntroductionAdviceResult<IEvent> IntroduceEvent(this IAdviser<INamedType> adviser, string eventTemplate, IntroductionScope scope = IntroductionScope.Default, OverrideStrategy whenExists = OverrideStrategy.Default, Action<IEventBuilder>? buildEvent = null, object? tags = null)
Parameters
Type Name Description
IAdviser<INamedType> adviser

An adviser for a named type.

string eventTemplate

The name of the event in the aspect class that must be used as a template for the introduced event. This event must be annotated with TemplateAttribute. The type of the template event can be any delegate type. The type of the introduced event can be changed dynamically thanks to the IEventBuilder returned by this method.

IntroductionScope scope

Determines the scope (e.g. Instance or Static) of the introduced event. The default scope depends on the scope of the template event. If the template event is static, the introduced event is static. However, if the template event is non-static, then the introduced event copies the scope of the target declaration of the aspect.

OverrideStrategy whenExists

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

Action<IEventBuilder> buildEvent

An optional delegate that modifies the IEventBuilder representing the introduced event.

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

An IIntroductionAdviceResult<T> exposing the introduced IEvent.

See Also

IntroduceEvent(IAdviser<INamedType>, string, string, string, string?, string?, IntroductionScope, OverrideStrategy, Action<IEventBuilder>?, object?, object?)

Introduces a new event to the target type, or overrides the implementation of an existing one, by specifying individual template methods for the adder and the remover, and optionally an invocation interception template.

Declaration
public static IIntroductionAdviceResult<IEvent> IntroduceEvent(this IAdviser<INamedType> adviser, string eventName, string addTemplate, string removeTemplate, string? invokeTemplate = null, string? raiseTemplate = null, IntroductionScope scope = IntroductionScope.Default, OverrideStrategy whenExists = OverrideStrategy.Default, Action<IEventBuilder>? buildEvent = null, object? args = null, object? tags = null)
Parameters
Type Name Description
IAdviser<INamedType> adviser

An adviser for a named type.

string eventName

The name of the introduced event.

string addTemplate

The name of the method of the aspect class whose type and implementation will be used as a template for the adder. This method must be annotated with TemplateAttribute. The signature of this method must be void Add(T value) where T is either dynamic or a type compatible with the type of the event.

string removeTemplate

The name of the method of the aspect class whose type and implementation will be used as a template for the remover. This method must be annotated with TemplateAttribute. The signature of this method must be void Remove(T value) where T is either dynamic or a type compatible with the type of the event.

string invokeTemplate

The name of the method of the aspect class whose type and implementation will be used as a template for intercepting invocation of the event's handlers. The signature of this method must be T Invoke(), T Invoke(U handler), or T Invoke(U handler, V1 param1, V2 param2, ...)
where T is either dynamic or a type compatible with the return value of the event's delegate type, U is either dynamic or the event's delegate type, Vn are types matching the delegate's parameters.

string raiseTemplate

Reserved for future use (not implemented).

IntroductionScope scope

Determines the scope (e.g. Instance or Static) of the introduced event. The default scope depends on the scope of the template event. If the template event is static, the introduced event is static. However, if the template event is non-static, then the introduced event copies the scope of the target declaration of the aspect.

OverrideStrategy whenExists

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

Action<IEventBuilder> buildEvent

An optional delegate that modifies the IEventBuilder representing the introduced event.

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

An IIntroductionAdviceResult<T> exposing the introduced or overriding IEvent.

Remarks

Unlike the overload that takes a template event, this method allows you to specify separate template methods for the add and remove accessors (and optionally an invoke template), providing complete control over the event's behavior. This is useful when you need custom logic for event subscription/unsubscription or handler invocation.

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

See Also