充分利用的InnerException(S)的所有消息? [英] Getting all messages from InnerException(s)?

查看:404
本文介绍了充分利用的InnerException(S)的所有消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法写一个LINQ的风格手短代码步行抛出的异常的InnerException(S)的各个层面?我宁愿把它写的地方,而不是调用了扩展功能(如下图),或继承例外类的。

 静态类扩展
{
公共静态字符串GetaAllMessages(此异常EXP)
{
字符串消息=的String.Empty;
异常的InnerException = EXP;


{
=信息+消息(string.IsNullOrEmpty(innerException.Message)的String.Empty:innerException.Message?);
的InnerException = innerException.InnerException;
}
,而(的InnerException!= NULL);

返回消息;
}
};


解决方案

不幸的是LINQ不提供,可以分层处理方法结构,只有集合。



其实,我有一些扩展方法,可以帮助做到这一点。我没有在手确切的代码,但他们是这样的:

  //检查离开了所有的错误简洁

//又名链接列表样式枚举
公共静态的IEnumerable< TSource> FromHierarchy< TSource>(
这TSource源,
Func键< TSource,TSource> nextItem,
Func键< TSource,布尔> canContinue)
{
为(VAR电流=源; canContinue(电流);电流= nextItem(当前))
{
产量返回电流;
}
}

公共静态的IEnumerable< TSource> FromHierarchy< TSource>(
这TSource源,
Func键< TSource,TSource> nextItem)
式TSource:类
{
返回FromHierarchy(来源nextItem,S = GT;!S = NULL);
}

然后在这种情况下,你可以做到这一点通过例外枚举:

 公共静态字符串GetaAllMessages(此异常除外)
{
变种的消息= exception.FromHierarchy(例如= GT ; ex.InnerException)
。选择(例如=> ex.Message);
返回的string.join(Environment.NewLine,消息);
}


Is there any way to write a LINQ style "short hand" code for walking to all levels of InnerException(s) of Exception thrown? I would prefer to write it in place instead of calling an extension function (as below) or inheriting the Exception class.

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;
    }
}; 

解决方案

Unfortunately LINQ doesn't offer methods that could process hierarchical structures, only collections.

I actually have some extension methods that could help do this. I don't have the exact code in hand but they're something like this:

// all error checking left out for brevity

// a.k.a., linked list style enumerator
public static IEnumerable<TSource> FromHierarchy<TSource>(
    this TSource source,
    Func<TSource, TSource> nextItem,
    Func<TSource, bool> canContinue)
{
    for (var current = source; canContinue(current); current = nextItem(current))
    {
        yield return current;
    }
}

public static IEnumerable<TSource> FromHierarchy<TSource>(
    this TSource source,
    Func<TSource, TSource> nextItem)
    where TSource : class
{
    return FromHierarchy(source, nextItem, s => s != null);
}

Then in this case you could do this to enumerate through the exceptions:

public static string GetaAllMessages(this Exception exception)
{
    var messages = exception.FromHierarchy(ex => ex.InnerException)
        .Select(ex => ex.Message);
    return String.Join(Environment.NewLine, messages);
}

这篇关于充分利用的InnerException(S)的所有消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆