public static SecureFormat ( IFormatProvider formatProvider, string format ) : string | ||
formatProvider | IFormatProvider | /// The format Provider. /// |
format | string | /// The format string. /// |
return | string |
public static string SecureFormat(IFormatProvider formatProvider, string format, params object[] args)
{
if (format == null)
{
// no format string
return string.Empty;
}
// no arguments, just use format
if (args == null)
{
return format;
}
try
{
return string.Format(formatProvider, format, args);
}
catch (FormatException)
{
string result = "!!! FORMAT ERROR !!!! " + format + ": ";
return args.Aggregate(result, (current, arg) => current + (arg + ", "));
}
}
public void SecureFormat_ReturnsErrorString_WhenTooFewParameters() { const string Format = "{0} {1} {2}"; const string Expected = "!!! FORMAT ERROR !!!! " + Format + ": "; const int I = 1; string result = FormatHelper.SecureFormat(CultureInfo.InvariantCulture, Format, I); result.Should().Be(Expected + I + ", ", "too few parameters."); }