3

Hi i have method getting the name of calling method:

    public static string GetMethodName()
    {
        System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(); 
        return trace.GetFrame(1).GetMethod().Name;
    }

And when I track my bugs and exceptions I always get method name .ctor how to avoid that or at least get something like ClassName<.ctor> ?

TrN
  • 1,230
  • 2
  • 17
  • 32

1 Answers1

2

How about:

StackTrace stackTrace = new StackTrace();
foreach(StackFrame sf in stackTrace.GetFrames())
{
   if (!sf.GetMethod().Name.StartsWith("."))    // skip all the ".ctor" methods
   {
       return sf.GetMethod().DeclaringType.Name + "." + sf.GetMethod().Name;
   }
}
return "??"; // What do you want here?

Using a string compare is a bit heavy-handed, but it works :)

dlchambers
  • 3,511
  • 3
  • 29
  • 34
  • It's really old question and I actually don't work on it anymore but when I find some time I'll try it :) – TrN Jul 13 '11 at 08:24