SecureFormat() 공개 정적인 메소드

Replacement for the String.Format method, that throws an exception when the count of arguments does not match the count of placeholders.

If format and/or arguments are null then still a string is returned.

Tries to format with String.Format. In case of an Exception the original format string and all parameters added in a list will be returned.
public static SecureFormat ( IFormatProvider formatProvider, string format ) : string
formatProvider IFormatProvider /// The format Provider. ///
format string /// The format string. ///
리턴 string
예제 #1
0
        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.");
        }
예제 #2
0
        public void SecureFormat_ReturnFormattedString_WhenCorrectNumberOfParameters()
        {
            const string  Format  = "{0} {1} {2}";
            const string  Correct = "1 1.25 string";
            const int     I       = 1;
            const decimal D       = 1.25m;
            const string  S       = "string";

            string result = FormatHelper.SecureFormat(CultureInfo.InvariantCulture, Format, I, D, S);

            result.Should().Be(Correct, "Correct number of parameters.");
        }
예제 #3
0
        public void SecureFormat_ReturnsFormattedString_WhenTooManyParameters()
        {
            const string  Format  = "{0} {1} {2}";
            const string  Correct = "1 1.25 string";
            const int     I       = 1;
            const decimal D       = 1.25m;
            const string  S       = "string";

            string result = FormatHelper.SecureFormat(CultureInfo.InvariantCulture, Format, I, D, S, I);

            result.Should().Be(Correct, "too much parameters.");
        }
예제 #4
0
 public void SecureFormat_ReturnsFormatString_WhenNoFormatParameters()
 {
     FormatHelper.SecureFormat(CultureInfo.InvariantCulture, "format", null)
     .Should().Be("format", "no args should result in format string.");
 }
예제 #5
0
 public void SecureFormat_ReturnsEmptyString_WhenFormatIsNull()
 {
     FormatHelper.SecureFormat(CultureInfo.InvariantCulture, null)
     .Should().Be(string.Empty, "null should result in empty string.");
 }