ReSharper Help

Defining Custom String Formatting Methods

ReSharper is able to validate string formatting methods, but in order to do so, it needs to know that a particular method takes a format string as a parameter. With standard methods, such as String.Format, Text.StringBuilder.AppendFormat, or Console.WriteLine, it is fairly obvious, but when a custom string formatting method appears, you have to tell ReSharper to interpret it as such - otherwise, ReSharper would not be able to make sure that all required arguments were passed.

Consider a custom string formatting method with the following signature:

public void ShowError(string formatParam, params object[] args) { // show error }

If the method is called incorrectly, as shown below, ReSharper would not warn about the missing argument:

stringFormattingMethods_01

To make ReSharper aware of a custom string formatting method

  1. Reference ReSharper code annotation attributes as described in the Annotations in Source Code section.
  2. Annotate your custom string formatting method with the StringFormatMethodAttribute attribute, which takes a single argument - the name of the format parameter:
    [StringFormatMethod("formatParam")] public void ShowError(string formatParam, params object[] args) { // show error }

ReSharper will now be able to warn you about missing arguments when this custom formatting method is invoked. Even more, the Add argument quick-fix will make it easier for you to insert them:

Quick-fix for the string formatting method call

See Also

Last modified: 27 August 2015