user1585345 user1585345 - 1 month ago 10
C# Question

Unable to define an extension method that recurses

I would like to define an extension method that recurses into a nested structure of, you guessed it, exceptions. This is what I would like to do...

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


But this generates a compiler error
'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?)


Could this be considered a compiler bug?

Answer

Your class utils should also be public static in order for it to be discoverable as an extension to System.Exception