Open sandboxFocus

Class MatchPointcut

Pointcut that selects the methods of the target type that have a given name, and applies the advice only to those whose signature matches the bound parameters of the advice.

Inheritance
MatchPointcut
Namespace: PostSharp.Aspects.Advices
Assembly: PostSharp.dll
Syntax
public sealed class MatchPointcut : Pointcut
Remarks

The aspect must be applied to a type, for instance a TypeLevelAspect. The pointcut selects the methods declared by this type whose name is MethodName, including the static and non-public ones. When MatchParameterCount is true, it also requires a given number of parameters.

The advice is then applied only to the selected methods whose signature matches its parameters bound by custom attributes derived from AdviceParameterAttribute. A method that does not match, for instance because an argument bound by ArgumentAttribute is missing or has an incompatible type, or because two bound values give conflicting type arguments to a generic advice, is skipped without error and left unchanged. Errors that do not come from the signature of the method, such as a violated generic constraint, are still reported. Several advices with the same pointcut can therefore handle the different overloads of a method.

Examples

The following advices of a TypeLevelAspect apply to the overloads Add(string) and Add(string, string) of the target type Catalog. An overload Add(int) matches none of them and is left unchanged.

[OnMethodEntryAdvice, MatchPointcut( nameof(Catalog.Add) )]
public static void OnAddItem( [Argument( 0 )] string item ) { }

[OnMethodEntryAdvice, MatchPointcut( nameof(Catalog.Add) )] public static void OnAddPair( [Argument( 0 )] string key, [Argument( 1 )] string value ) { }

Constructors

Name Description
MatchPointcut(string)

Initializes a new instance of the MatchPointcut class.

Properties

Name Description
MatchParameterCount

Gets or sets a value indicating whether the pointcut selects only the methods whose number of parameters is the highest index of the parameters of the advice bound by ArgumentAttribute(int), plus one. The default value is true.

MethodName

Gets the name of the methods of the target type to which the advice can apply.

See Also