In C#, is there a function that returns the current class/method name?
Yes, It is allowed to define a method with the same name as that of a class.
We can have a method name same as a class name in Java but it is not a good practice to do so. This concept can be clear through example rather than explanations. In the below example, a default constructor is called when an object is created and a method with the same name is called using obj.
The current method name that contains the execution point that is represented by the current stack trace element is provided by the java. lang. StackTraceElement. getMethodName() method.
Method Class | getName() Method in Java Method class is helpful to get the name of methods, as a String. To get name of all methods of a class, get all the methods of that class object. Then call getName() on those method objects. Return Value: It returns the name of the method, as String.
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.
System.Reflection.MethodBase.GetCurrentMethod()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With