I am trying to combine the C# 5.0 Caller Information along with the C# params keyword. The intention is to create a wrapper for a logging framework, and we want the logger to format the text like String.Format. In previous versions, the method looked like this:
void Log(
string message,
params object[] messageArgs = null);
And we call it like this:
log.Log("{0}: I canna do it cap'n, the engines can't handle warp {1}!",
"Scotty", warpFactor);
Now, we want to capture caller information and log that as well. So the signature becomes:
void Log(
string message,
params object[] messageArgs,
[CallerMemberName] string sourceMemberName = null);
That doesn't compile because the params must be last parameter. So I try this:
void Log(
string message,
[CallerMemberName] string sourceMemberName = null,
params object[] messageArgs);
Is there a way to call that without either providing sourceMembername, or assigning the messageArgs argument explicitly as a named parameter? Doing so defeats the purpose of the params keyword:
// params is defeated:
log.Log("message",
messageArgs: new object[] { "Scotty", warpFactor });
// CallerMemberName is defeated:
log.Log("message", null,
"Scotty", warpFactor);
Is there a way to do this? It seems like the "hacky" way the caller information is passed precludes using the params keyword. It would be awesome if the C# compiler recognized that the caller member information parameters aren't really parameters at all. I can see no need to ever pass them explicitly.
My backup will be to skip the params keyword, and the caller will have to use the long signature in that last example.
[CallerMemberName]
attribute. – Stick