开发者

How to get real method name istead of .ctor?

Hi i have method getting the name of calling method:

    public static string GetMethod开发者_JAVA技巧Name()
    {
        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> ?


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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜