You may find it waste.Why somebody need to find out the name of the method by writing code inside the same method? But I have a scenario where I need to find out who is calling one method inside it. In other words from a method’s perspective I need to know who (method name ,class & assembly)called me and based on that I want to do some actions.Something like prevent calls from some particular classes & assemblies.
Still I didn’t get any luck in this.But during the google I found out another thing. How to know the name of the method through the code written inside the same. It was interesting to me and here are the 2 ways.
static void PrintMyName()
{
Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name);
Console.WriteLine(new StackFrame(0).GetMethod().Name);
}
This will display the method name to which the code belongs “PrintMyName”.
No comments:
Post a Comment