getClassName
can any body tell plz how can we get the class name in C#?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Mohamed IhjasPosted Dec 13, 2007, 5:35 AM
AlanPosted Dec 13, 2007, 5:11 AM
If you want to get the name of the class in which the code is currently executing, then you can do it like this:
using System;
namespace Test
{
class Program
{
static void Main()
{
Program p = new Program();
string fullName = p.GetClassName(true);
string name = p.GetClassName(false);
Console.WriteLine("Full name of class is {0}", fullName);
Console.WriteLine("Name of class is {0}", name);
Console.ReadLine();
}
public string GetClassName(bool full)
{
string name = this.GetType().ToString();
if (full)
return name;
return name.Substring(1 + name.LastIndexOf('.'));
}
}
}