是否有任何方法可以编写LINQ样式的"简写"代码,用于遍历抛出Exception的所有级别的InnerException?我宁愿写它而不是调用扩展函数(如下所示)或继承Exception类.
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;
}
};
Run Code Online (Sandbox Code Playgroud) 我已阅读MSDN但我无法理解这个概念.
如果我错了,纠正我,
当前异常将使用innerexception.
首先发生内部异常,然后发生当前异常(如果有异常),这InnerException就是检查的原因null.为了保留内部异常,我们必须将其作为参数传递.
我对吗?