Is there a function that returns the current class/method name?

C#

C# Problem Overview


In C#, is there a function that returns the current class/method name?

C# Solutions


Solution 1 - C#

Current class name:

this.GetType().Name;

Current method name:

using System.Reflection;

// ...

MethodBase.GetCurrentMethod().Name;

Since you're using this for logging purposes, you may also be interested in getting the current stack trace.

Solution 2 - C#

Solution 3 - C#

System.Reflection.MethodBase.GetCurrentMethod().DeclaringType

Solution 4 - C#

I changed the above example a little into this piece of working example code:

public class MethodLogger : IDisposable
{
    public MethodLogger(MethodBase methodBase)
    {
        m_methodName = methodBase.DeclaringType.Name + "." + methodBase.Name;
        Console.WriteLine("{0} enter", m_methodName);
    }

    public void Dispose()
    {
        Console.WriteLine("{0} leave", m_methodName);
    }
    private string m_methodName;
}

class Program
{
    void FooBar()
    {
        using (new MethodLogger(MethodBase.GetCurrentMethod()))
        {
            // Write your stuff here
        }
    }
}

Output:

Program.FooBar enter
Program.FooBar leave

Solution 5 - C#

Yes! The MethodBase class's static GetCurrentMethod will inspect the calling code to see if it's a constructor or a normal method, and returns either a MethodInfo or a ConstructorInfo.

This namespace is a part of the reflection API, so you can basically discover everything that the run-time can see by using it.

Here you will find an exhaustive description of the API:

http://msdn.microsoft.com/en-us/library/system.reflection.aspx

If you don't feel like looking through that entire library here is an example I cooked up:

namespace Canvas
{
  class Program
  {
    static void Main(string[] args)
    {
        Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod());
        DiscreteMathOperations viola = new DiscreteMathOperations();
        int resultOfSummation = 0;
        resultOfSummation = viola.ConsecutiveIntegerSummation(1, 100);
        Console.WriteLine(resultOfSummation);
    }
}

public class DiscreteMathOperations
{
    public int ConsecutiveIntegerSummation(int startingNumber, int endingNumber)
    {
        Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod());
        int result = 0;
        result = (startingNumber * (endingNumber + 1)) / 2;
        return result;
    }
}    
}

The output of this code will be:

Void Main<System.String[]> // Call to GetCurrentMethod() from Main.
Int32 ConsecutiveIntegerSummation<Int32, Int32> //Call from summation method.
50 // Result of summation.

Hope I helped you!

JAL

Solution 6 - C#

You can get the current class name, but I can't think of anyway to get the current method name. Though, the names of methods for the current can be obtained.

string className = this.GetType().FullName;
System.Reflection.MethodInfo[] methods = this.GetType().GetMethods();
foreach (var method in methods)
    Console.WriteLine(method.Name);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCraig JohnstonView Question on Stackoverflow
Solution 1 - C#CameronView Answer on Stackoverflow
Solution 2 - C#GabeView Answer on Stackoverflow
Solution 3 - C#sajoshiView Answer on Stackoverflow
Solution 4 - C#Jan WilmansView Answer on Stackoverflow
Solution 5 - C#Jose AlavaView Answer on Stackoverflow
Solution 6 - C#Fun Mun PiengView Answer on Stackoverflow