Search code examples
c#string-formattingoptional-parametersc#-5.0

Combining CallerMemberName with params


Right now (C# 4.0), our logging method looks like

public void Log(string methodName, string messageFormat, params object[] messageParameters)

where the logger does the string formatting, so that the caller does not have to put String.Format's to create a nice log message (and allows for the logger to skip the string formatting if no logviewer is attached).

With C# 5.0, I would like to get rid of the methodName parameter by using the new CallerMemberName attribute but I don't see how this can be combined with the 'params' keyword. Is there a way to do this?


Solution

  • I believe you simply can't combine params and optional parameters, which are required for CallerMemberName. The best you can do is to use actual array instead of params.