有没有什么办法可以写出一个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