Published · 1 min read

Get the name of the currently executing Function/Method

A StackFrame is created and pushed on the call stack for every function call made during the execution of a thread.

A StackFrame is created and pushed on the call stack for every function call made during the execution of a thread. The stack frame always includes MethodBase information, and optionally includes file name, line number, and column number information.

Source: MSDN

System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
System.Reflection.MethodBase mb = sf.GetMethod();
Console.WriteLine(mb.Name);

This code can be used to get the name of the current class.

string ClassName = this.GetType().Name;

Note: This code can only be called inside a non-static method/function. This is because a static method cannot put into use the instance retriever – this keyword.

Partho Sarathi

Partho Sarathi

Founder, Maxotek

C# & .NET

Our blog

Related posts

Tutorials, notes and news from Maxotek.

View all posts

See what Maxotek makes

Monitoring, conversion tools and more, built by the same people who wrote these posts.