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
Dumbo
  • 13,555
  • 54
  • 184
  • 288

3 Answers3

10

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,918
  • 22
  • 68
  • 123
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
  • 30,108
  • 19
  • 83
  • 131