2012-05-12 68 views
0

這是string.Format上的錯誤還是什麼?爲什麼會拋出System.FormatException?

// 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); 
} 
+0

什麼是模型和model.Username? –

+3

什麼是'FormatWith'?它不是BCL'字符串'擴展方法。 – Oded

+0

您可以粘貼FormatWith擴展方法的代碼嗎? – ivowiblo

回答

6

您的問題是,試圖在格式字符串使用{}。他們需要被轉義,否則他們將被視爲格式變量。

更改最後一行是:

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); 
相關問題