Thursday, 11 November 2010

Dynamically Determine the Current Method / Function in C# and VB.NET

Needed to do some basic reflection the other day to easily determine the current method that was being executed in a VB ASP.NET application.

So here's the C# for it:

//Current method name
System.Reflection.MethodBase.GetCurrentMethod()
    .Name;

//Fully qualified name of the method's class
System.Reflection.MethodBase.GetCurrentMethod()
    .ReflectedType.FullName;

//Method's class name without Namespace
System.Reflection.MethodBase.GetCurrentMethod()
    .ReflectedType.Name;

//Namespace
System.Reflection.MethodBase.GetCurrentMethod()
    .ReflectedType.Namespace;

And the VB.NET:

'Current method name
System.Reflection.MethodBase.GetCurrentMethod(). _
    Name

'Fully qualified name of the method's class
System.Reflection.MethodBase.GetCurrentMethod(). _
    ReflectedType.FullName

'Method's class name without Namespace
System.Reflection.MethodBase.GetCurrentMethod(). _
    ReflectedType.Name

'Namespace
System.Reflection.MethodBase.GetCurrentMethod(). _
    ReflectedType.Namespace

No comments:

Post a Comment