I have a situation where in some context I want to pass an explicit value to my method with a parameter marked up with CallerMemberName, and from other contexts I want it to automatically capture. I want to know if this is possible, or will the attribute always take priority?
Why would I want to do that, you ask. I am writing an auditing component that can be called from anywhere. It takes some parameters required for the auditing context, as well as the CallerMemberName param. But on some of my super classes I already have certain bits of information relevant to the auditing, so I wrote a method there that only takes the CallerMemberName param. It then calls through to my auditing component passing the parameters it already has in it's context as well as the caller member name it already implicitly captured.
Yes, it is possible, so it can be used both implicitly and explicitly.
private void CaptureCaller(
[CallerMemberName] string callerMemberName = "")
{
// callerMemberName will contain the name of the method that called CaptureCaller if called with no parameters.
// callerMemberName will contain the value of the passed in parameter if provided.
}
CaptureCaller();
CaptureCaller("custom value");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With