Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you override the automatically captured value of a parameter attributed with CallerMemberName by explicitly passing a value?

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.

like image 711
Jacques Bosch Avatar asked Nov 01 '13 05:11

Jacques Bosch


1 Answers

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");
like image 163
Jacques Bosch Avatar answered Nov 03 '22 21:11

Jacques Bosch