TemplateIsNotCompileTimeConstant should ignore NameOf()
See original GitHub issueHiya,
I often have messagetemplates like:
_logger.LogError(e, $"Caught exception in {nameof(PostStock)}");
Where instead of typing PostStock
as constant string, I tend to use NameOf
which is useful because the name may change.
This could also include SizeOf - but i’m not sure whether that is a compile time variable as NameOf is…
The docs mention ‘constants’ but still…
Related:
I would love if the warning wouldn’t show up in the case of NameOf.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
"Should be a compile time constant" warning when using ...
Yes, the nameof expression is evaluated at compile-time, but the interpolation is not. The compilation of this code fails. const string a =...
Read more >CA1507: Use nameof instead of string (code analysis) - .NET
Rule CA1507 flags the use of a string literal as an argument to a method or constructor where a nameof ( NameOf in...
Read more >Code Inspection: Use 'nameof' expression to reference name
The nameof operator, added in C# 6.0, addresses this — it allows capturing the string names of symbols that are in the scope....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Seems you’re right!
The following works:
I’ll use that from now on. Thanks for your support! (would be a cool refactor action to add btw)
Lol I thought the latest version was c#9 for some reason. Funny I’ll try this out tomorrow although I’ve now gotten used to writing logs in the concatenated version 🙃