CSharpEssentials icon indicating copy to clipboard operation
CSharpEssentials copied to clipboard

Add string interpolation conversions for other common .NET APIs

Open DustinCampbell opened this issue 10 years ago • 5 comments

It'd be great to be able to apply the "Convert to String Interpolation" to .NET APIs other than just String.Format(). For example:

  • Debug.WriteLine(string, params object[])
  • Console.WriteLine(string, params object[])
  • StringBuilder.AppendFormat() (even though this causes an allocation)

DustinCampbell avatar Jul 21 '15 18:07 DustinCampbell

Add to the list TextWriter.Write and TextWriter.WriteLine

pharring avatar Jul 21 '15 19:07 pharring

Or user-defined methods as well.

fsateler avatar Jul 22 '15 19:07 fsateler

@fsateler, Do you have a proposal for how that might be detected? Requiring a parameter of type string and a parameter of object[] seems too permissive.

DustinCampbell avatar Jul 22 '15 19:07 DustinCampbell

@DustinCampbell I think the only reasonable way is to require annotation with an attribute. This would also allow formatters that require extra parameters:

 [FormatStringConsumer(FormatStringParameterNumber = 2)]
 void WriteToDevice(Device d, string format, params object[] arguments) { ... }

fsateler avatar Jul 22 '15 19:07 fsateler

Nice idea! Should probably be a separate issue though. Added #21 to capture this.

DustinCampbell avatar Jul 22 '15 20:07 DustinCampbell