GetClassName
can any body tell plz how can we get the class name in C#?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
AlanRank #669 · 3.6K reputationPosted Dec 13, 2007, 12:11 AM EST
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('.'));
}
}
}
Mohamed Ihjas14 reputationPosted Dec 13, 2007, 12:35 AM EST