Reputation: 1855
CA2241: Provide correct arguments to formatting methods works great unless you are using string resources for the format string argument.
Does anyone know of a custom rule out there which does the same thing for String.Format calls which use a resource for the string format?
For example, instead of this (which rule CC2241 flags):
String.Format( "{0} {1}", value );
I need a rule that detects the problem where Resources.MessageWithTwoPlaceholders is a resource defined as "{0} {1}".
String.Format( Resources.MessageWithTwoPlaceholders, arg );
I've taken a look at writing a custom rule to handle this, and looks doable, but a fairly significant effort.
Upvotes: 1
Views: 872
Reputation: 114917
The problem with this type of rule is that the format string could be wrong in more than one assembly, and not necessarily the Assembly being analyzed.
What makes it even harder, is that you're not loading the resource from a resource manager, but from a generated resource class, which abstracts away the name of the resource file, the name of the resource name, and the actual value of the resource.
Another issue you'll run into is that loading the resource data into memory probably requires you to actually load the assembly for real, instead of just doing introspection on it, unless you want to parse the resource yourself.
Finally, in many translation conditions, the resource files are built at development time with just one language, and translations are post-compiled and added to the product at a later stage...
With all that said, it should be possible. Finding the content is hard, but the actual rule can then be copied from the existing code.
Upvotes: 1