有沒有什麼辦法可以寫出一個LINQ風格的「短手」代碼,用於步行到拋出異常的InnerException(s)的所有級別?我寧願寫它而不是調用一個擴展函數(如下)或繼承Exception
類。從InnerException(s)獲取所有消息?
static class Extensions
{
public static string GetaAllMessages(this Exception exp)
{
string message = string.Empty;
Exception innerException = exp;
do
{
message = message + (string.IsNullOrEmpty(innerException.Message) ? string.Empty : innerException.Message);
innerException = innerException.InnerException;
}
while (innerException != null);
return message;
}
};
我可以問你爲什麼要使用比擴展方法別的東西嗎?你的代碼對我來說看起來很好,並且可以在代碼中的任何地方重用。 – ken2k 2012-02-16 15:45:44
@ ken2k:雖然你不想按照他現在的方式構建消息...... – 2012-02-16 16:11:20
@JeffMercado是的,但是「擴展方法」的概念有什麼問題? – ken2k 2012-02-16 16:12:46