0

これはバグですか、string.Formatそれとも何ですか?

// Act
string l = "This is an UnitTest embedded resource.";
string r = "Please do not remove or change. Sincerely yours, {0}".FormatWith(model.Username);
string expected = "[\r\n\t{0}\r\n\r\n\r\n]\r\n{\r\n\t\r\n\t{1}\r\n\r\n}".FormatWith(l, r);

このFormatWithメソッドは、シンタックス シュガーの単なる拡張です。

public static string FormatWith(this string text, params object[] args)
{
    return string.Format(text, args);
}
4

1 に答える 1

6

あなたの問題は、フォーマット文字列で{andを使用しようとすることです。}エスケープする必要があります。そうしないと、フォーマット変数として扱われます。

最後の行を次のように変更します。

string expected = "[\r\n\t{0}\r\n\r\n\r\n]\r\n{{\r\n\t\r\n\t{1}\r\n\r\n}}".FormatWith(l, r);
于 2012-05-12T17:54:13.920 に答える