0

I don't know it is possible or not, but if there is some way to do this then please suggest. I have one method and I am call this method with different classes, so how will I able to know(Class Name) into method, that from which method is called?

Below is my scenario,

public class Employee()
{
    protected bool Check()
    {
         //Here how will I know method name(Test1 or Test2) from which call is made? Please note I can't use any parameter with Check method.
    }
    public void Test1()
    {
         Check();
    }
    public void Test2()
    {
         Check();
    }
}
Hrdk
  • 83
  • 1
  • 8

4 Answers4

2

You can try this:

string myClass = MethodBase.GetCurrentMethod().DeclaringType.Name;

And in C# 6.0 then you can use nameof

nameof(YourMethodname)
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

You could use

var className = this.GetType().Name;
R Day
  • 962
  • 9
  • 25
0

You can use the StackTrace;

StackTrace stackTrace = new StackTrace();
MethodBase methodBase = stackTrace.GetFrame(1).GetMethod();
Console.WriteLine(methodBase.Name);

Thanks to Jason

Community
  • 1
  • 1
Irshad
  • 3,071
  • 5
  • 30
  • 51
-1

You will find your answer here

using System.Diagnostics;

// get call stack
StackTrace stackTrace = new StackTrace();

// get calling method name
Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);