3

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);
}       
Community
  • 1
  • 1
Saeid Yazdani
  • 13,065
  • 50
  • 172
  • 279

3 Answers3

9

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.

Community
  • 1
  • 1
horgh
  • 17,099
  • 20
  • 62
  • 120
0
public string GetClassName(object item)
{
  return typeof(item).Name;
}
mdcuesta
  • 1,739
  • 1
  • 15
  • 17
0

Try following:

this.GetType().Name

Type.Name -> Gets the name of the current member.

Tilak
  • 28,854
  • 17
  • 79
  • 129