Open sandboxFocusImprove this doc

PS0208: Parameter of State Machine Optimized Out

Severity Warning
Reported by PostSharp
Message The parameter '<Parameter>' of method '<Method>' has been optimized out by the compiler and will not be available to the aspect. Disable compiler optimizations to access the parameter.

Cause

The C# compiler moves the body of an async or iterator method into the MoveNext method of a state machine, and copies the parameters of the method into fields of the state machine. When it optimizes the code, as in the Release configuration, it omits the fields of the parameters that the body does not use.

When an aspect transforms the state machine, for example an OnMethodBoundaryAspect that reads Arguments in an async method (see Semantic Advising of Iterator and Async Methods), PostSharp reads the parameters from these fields. Before it does, it adds a field for each parameter that the compiler omitted and copies the argument into it. PostSharp reports PS0208, on the method, only when a parameter still has no field afterwards. The aspect then does not see the value of this parameter.

PostSharp adds the missing fields itself in the usual cases, so no example can show this message.

Resolution

  • Disable the optimizations of the project, for the configurations where the aspect must see the parameter, with the Optimize MSBuild property:

    <PropertyGroup Condition="'$(Configuration)' == 'Release'">
      <Optimize>false</Optimize>
    </PropertyGroup>
    
  • Or use the parameter in the body of the method, so that the compiler keeps it.

  • When the aspect does not need the parameter, ignore the warning, or disable it with the PostSharpDisabledMessages MSBuild property. See Well-Known MSBuild Properties.

See Also

Reference

OnMethodBoundaryAspect
MethodExecutionArgs

Other Resources

Semantic Advising of Iterator and Async Methods
Well-Known MSBuild Properties