-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal]: Allow nameof(method parameter) outside the method. #4413
Comments
Would you be able to access members of the parameter as well? E.g. in your example |
Probably depends on the ease of implementation. There is already a cleaner way to do that ( |
The syntax of using member access to reference a parameter of a method feels really weird here, as does the name resolution (although you could argue that the latter is already "broken" by method group resolution). It gets much weirder if you can dot into the members of the parameter type, especially if there are overloads of that method that could have parameters of the same name but of different types. What are the use cases where you'd want to take the name of a method parameter in a separate method body? |
The use case that come to mind is mostly for analyzers, such as the example provided.
That's correct. In that case I think accessing into the members of the parameter should be completely disallowed.
Could we consider another token other than the DotToken? |
One use case is to register specific parameter value during dependency injection registration. builder.RegisterType<Processor>()
.As<IProcessor>()
.WithParameter("timeout", config.Timeout); Note that it requires nameof access to constructor parameters though. |
Will this support parameters of methods in other types? const string s = nameof(My.Namespace.Class.Method.parameter); |
Currently, there is no proposal for adding such semantics. |
Another example of usage would be attributes. For instance, in the Open API & Azure Functions attributes /*...*/
[OpenApiParameter(name: nameof(FuncClass.SomeFunction.someParam), In = ParameterLocation.Path /*, ... */)]
/*...*/
public async Task<IAsyncResult> SomeFunction(
[HttpTrigger(/*...*/ Route=$"abc/{{{nameof(FuncClass.SomeFunction.someParam}}}/abc")] HttpRequest request,
string someParam)
{
/* ... */
} Even better would be if some of the context could be inferred so that the class or method name don't need to be applied based on the target of the attribute. |
Never mind, I should have read the proposal in more detail as the use in attributes is in #373 |
Allow nameof(method_parameter_name)
Summary
Currently, there is no way to use a parameter name outside the method without hardcoding it.
Motivation
Extending
nameof
capabilities and allowing it to be used in more contexts. Similar to: #4384, and extends #373 (not a dupe of it)Detailed design
While #373 suggests allowing
nameof
for parameter names in attributes applied to the method, this proposal is a suggestion to generalize it to be allowed outside the method context. A real-world example that can benefit from this:https://github.com/dotnet/roslyn-analyzers/blob/b76ff2f/src/Microsoft.CodeAnalysis.Analyzers/Core/MetaAnalyzers/DiagnosticDescriptorCreationAnalyzer.cs#L29-L35
Drawbacks
Alternatives
Unresolved questions
Design meetings
The text was updated successfully, but these errors were encountered: