Proposal: CallerTypeNameAttribute to provide nameof class/struct that is calling method. #102724
Unanswered
nathan130200
asked this question in
Ideas
Replies: 1 comment
-
There is a discussion in the C# Language repo with a similar proposal: dotnet/csharplang#87 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
As the title says, the introduction of the attribute
[CallerTypeName]
which will provide the method with the name of the type that is invoking the current method, as well as[CallerMemberName]
. Used especially in logging functions.In asynchronous functions (context: async/await/task) the value will receive the original type that invoked this function.
Example:
typeName
will benameof(Utilities)
same behaviour asmemberName
that will beWriteToStdout
.I had this problem while writing a micro logger to handle specific tasks with more logging details. And when I realized that I was either creating a gigantic work-around or creating scoped loggers that store the methodbase instance or method/type name.
And as C# does not support expansive macros like C++, it is not possible to simply write a macro that calls all the necessary functions by passing
MethodBase.GetCurrentMethod()
in each of them. (it would be something like#define INVOKE_LOG(LogInst, Msg) LogInst.WriteLine(Msg, MethodBase.GetCurrentMethod())
)Beta Was this translation helpful? Give feedback.
All reactions