Is it possible to print the class name from within a static function?
e.g ...
public class foo { static void printName() { // Print the class name e.g. foo } }
You have three options to get the type (and therefore the name) of YourClass
that work in a static function:
typeof(YourClass)
- fast (0.043 microseconds)
MethodBase.GetCurrentMethod().DeclaringType
- slow (2.3 microseconds)
new StackFrame().GetMethod().DeclaringType
- slowest (17.2 microseconds)
If using typeof(YourClass)
is not desirable, then MethodBase.GetCurrentMethod().DeclaringType
is definitely the best option.
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