Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Current Class Name [duplicate]

Tags:

c#

wpf

In Android, if I need to get name of current class, i can do something like:

private final _TAG = DummyActivity.this.getClass().getSimpleName();

this would return "DummyActivity"

I want to do same in C# WPF app code-behind. How do I get the name of current class?

this.GetType().Name; //this works only on instances of a class 

Looks like the only option is to hard-code it in C# like this:

private const string _TAG = "DummyWindow";
like image 203
pixel Avatar asked May 19 '26 09:05

pixel


1 Answers

You can do it like this in C#:

private static string _TAG = MethodBase.GetCurrentMethod().DeclaringType.Name;

This will work because initializing this field actually happens in the static constructor. I.e., MethodBase.GetCurrentMethod() returns the static constructor of the class.

like image 89
Yacoub Massad Avatar answered May 21 '26 01:05

Yacoub Massad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!