Open sandboxFocus

Class ReturnValueAttribute

Binds a parameter of a method interception or method boundary advice to the return value of the target method.

Inheritance
ReturnValueAttribute
Namespace: PostSharp.Aspects.Advices
Assembly: PostSharp.dll
Syntax
public sealed class ReturnValueAttribute : AdviceParameterAttribute
Remarks

The type of the parameter must be the type of the return value, or a type parameter of the advice method. In an async method that a method boundary advice applies to as a state machine, and in a non-async method that returns a task, the return value is the result of the task. An advice can have only one parameter bound by this attribute.

The meaning of the parameter depends on the advice:

  • In an OnMethodInvokeAdvice, the parameter must be out or ref, and the value that the advice assigns to it is the value returned by the target method. An advice applied to a method that returns a value must have either a parameter bound by this attribute or a MethodInterceptionArgs parameter.
  • In an OnMethodSuccessAdvice or an OnMethodExitAdvice, the advice reads the value that the method returns, by value or in, or replaces it with ref. When the method throws, the exit advice reads the default value.
  • In an OnMethodEntryAdvice or an OnMethodExceptionAdvice, the parameter must be out or ref. It sets the value that the method returns when the advice sets the flow behavior to Return.

In a method boundary advice, an out or ref parameter takes precedence over ReturnValue when the advice has both.

Constructors

Name Description
ReturnValueAttribute()

Properties

Name Description
IsRequired

Gets or sets a value indicating whether the target method of a method boundary advice must have a return value. When this property is true, the compiler reports an error when the method returns void, returns a task without a result, returns by reference (ref or ref readonly), or is an iterator. When it is false, the parameter receives the default value of its type in these methods, and a value that the advice writes to an out or ref parameter is ignored. The default value is false.

See Also