csharplang/proposals/extended-nameof-scope.md
2019-12-27 12:55:49 -08:00

2.2 KiB

Extended nameof scope

Summary

Allow nameof(parameter) inside an attribute on a method or parameter. For example:

  • [MyAttribute(nameof(parameter))] void M(int parameter) { }
  • [MyAttribute(nameof(TParameter))] void M<TParameter>() { }
  • void M(int parameter, [MyAttribute(nameof(parameter))] int other) { }

Motivation

Attributes like NotNullWhen or CallerExpression need to refer to parameters, but those parameters are currently not in scope.

Detailed design

Methods

The method's type_parameters are in scope throughout the method_declaration, and can be used to form types throughout that scope in return_type, method_body, and type_parameter_constraints_clauses but not in attributes, except within a nameof expression in attributes.

Method parameters

Names are introduced into this declaration space by the type parameter list and the formal parameter list of the method in nameof expressions in attributes placed on the method or its parameters.

Within a nameof expression in attributes placed on the method or its parameters, formal parameters can be referenced by their identifiers in simple_name expressions.

Simple names

If K is zero and the simple_name appears within a nameof expression TODO... block and if the block's (or an enclosing block's) local variable declaration space (Declarations) contains a local variable, parameter or constant with name I, then the simple_name refers to that local variable, parameter or constant and is classified as a variable or value.

If K is zero and the simple_name appears within the body of a generic method declaration and if that declaration includes a type parameter with name I, then the simple_name refers to that type parameter.

TODO

Scopes