Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting class name inside the class itself [duplicate]

Tags:

c#

Possible Duplicate:
C# getting its own class name

For Visual Studio, I want to create a class template and use a string field that holds the name of a class itself. Is it possible?

For example:

public class MyClass
{
     public string TAG = GetClassName(this);
}       
like image 751
Saeid Yazdani Avatar asked Sep 01 '26 06:09

Saeid Yazdani


1 Answers

When talking about non-static methods use Object.GetType Method which returns the exact runtime type of the instance (a reference to an instance of the Type Class):

this.GetType().Name

When talking about static methods, use MethodBase Class and its GetCurrentMethod Method :

Type t = MethodBase.GetCurrentMethod().DeclaringType;
//t.Name 

However, see this post on SO for more info on this.

like image 117
horgh Avatar answered Sep 02 '26 20:09

horgh



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!