2016-11-08 47 views
1

我想定义一个扩展方法,该方法递归到嵌套结构中,您猜对了异常。这是我想怎么办...无法定义递归的扩展方法

class Utils 
{ 
    public static string TellMeEverything(this Exception ex) 
    { 
     return ex.InnerException?.TellMeEverything() + ex.Message + ex.StackTrace; 
    } 
} 

但这生成编译器错误'Exception' does not contain a definition for 'TellMeEverything' and no extension method 'TellMeEverything' accepting a first argument of type 'Exception' could be found (are you missing a using directive or an assembly reference?)

难道这被认为是一个编译器错误?

+3

不应该'?.'访问是对'InnerException'而不是'ex'?如果'ex'为null,你已经保护了内部的'TellMeEverything'调用,但是你仍然会在'ex.Message'上冒充空引用。 –

+0

哦,我的天啊,我必须醒来。 – bbsimonbb

回答

5

class utils也应该public static,以便它能够被发现的一个扩展System.Exception