Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support for method parameter names in nameof() #373

@taori

Description

@taori

Update (jcouv): speclet (https://github.com/dotnet/csharplang/blob/main/proposals/csharp-11.0/extended-nameof-scope.md)


image

it would be fairly amazing if nameof also worked for MethodName arguments.

I think i've talked about this 1-2 years ago and some aspnet member also chimed in and mentioned how this could be useful for magic EntityFramework where the appropriate name must be passed too for connection strings to work.

So essentially i would love it if, instead of strings, i could be typing this:

[AuthGrantFilter(
  AppFunction = ApplicationFunctionTypes.ReadProjectStructure, 
  TopLevelProjectId = nameof(topLevelProjectId), 
  SiteId = nameof(siteId))]
[AuthDenyFilter(
  Context = DenyContext.Site, 
  TopLevelProjectId = nameof(topLevelProjectId), 
  SiteId = nameof(siteId))]

or this

[AuthGrantFilter(AppFunction =
  ApplicationFunctionTypes.ReadProjectStructure, 
  TopLevelProjectId = nameof(GetChildrenOf.topLevelProjectId), 
  SiteId = nameof(GetChildrenOf.siteId))]
[AuthDenyFilter(
  Context = DenyContext.Site, 
  TopLevelProjectId = nameof(GetChildrenOf.topLevelProjectId), 
  SiteId = nameof(GetChildrenOf.siteId))]

Update (jcouv):
This proposal (in its most likely and straightforward design) would affect scoping rules and thus would introduce a breaking change. It is worth considering whether this breaking change is severe. Here's an example to illustrate the problem:

const int p = 3;

[Attribute(Property = p)] // currently binds to the constant, but the parameter `p` would be in scope after this proposal
void M(int p) { }

Some alternatives for LDM to consider:

  1. Change the scoping rules and take the breaking change
  2. Only affect the scoping rules for nameof
  3. Allow nameof to reference parameters with the method name as a qualifier [Attr(nameof(M.p)] void M(int p) { }

LDM history:

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions