forum

Home / DeveloperSection / Forums / Getting current class name including parent class name

Getting current class name including parent class name

Jayden Bell 1779 29-Mar-2014

If I have a class B and C which inherits from A, is there something simpler than using StackFrame's GetFileName() (then parse out the ClassName.cs string)?

If I use this.GetType().Name, it won't return "A" when the code is executing in the parent class.

Sample Code

namespace StackOverflow.Demos
{
    class Program
    {
        public static void Main(string[] args)
        {
            B myClass = new C();
            string containingClassName = myClass.GetContainingClassName();            Console.WriteLine(containingClassName); //should output StackOverflow.Demos.B
            Console.ReadKey();
        }
    }
public class A {public A() { } }
public class B : A { public B() { } }
public class C : B { public C() { } }
}

c# c# 
Updated on 29-Mar-2014

Can you answer this question?


Answer

1 Answers

Liked By