简体   繁体   中英

Getting current class name including parent class name?

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() { } }

}
var yourType = GetType();
var baseType = yourType.BaseType;

or alternatively:

var currentTypeName = new StackFrame(1, false).GetMethod().DeclaringType.Name;

I use myObject.GetType().FullName which returns the fully qualified name of the class. For your scenario, you could use myObject.GetType().BaseType.FullName

Option 1 :: Getting the "container" class:

/**CODE**/

    public static void Main(string[] args)
    {
        B c = new C();
        Test(c);
        Console.ReadKey();
    }
    public static void Test(A a) { Console.WriteLine(typeof(A).ToString()); }
    public static void Test(B b) { Console.WriteLine(typeof(B).ToString()); }
    public static void Test(C c) { Console.WriteLine(typeof(C).ToString()); }

/**OUTPUT**/

    StackOverflow.Demos.B

Option 2 :: Getting the "container" class:

/**CODE**/

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

    public interface IGetContainerType
    {
        Type GetContainerType();
    }

    public class A: IGetContainerType 
    { 
        public A() { }
        public Type GetContainerType()
        {
            return typeof(A);
        }
    }
    public class B : A
    {
        public B() { }
        public new Type GetContainerType()
        {
            return typeof(B);
        }
    }
    public class C : B
    {
        public C() { }
        public new Type GetContainerType()
        {
            return typeof(C);
        }
    }

/**OUTPUT**/

    StackOverflow.Demos.B

Answer to similar but different question; Getting the instance class / full hierarchy:

/**CODE**/

    class Program
    {

        public static void Main(string[] args)
        {
            C c = new C();
            Type myType = c.GetType();
            while(myType != null)
            {
                Console.WriteLine(myType);
                myType = myType.BaseType;
            }
            Console.ReadKey();
        }
    }


/**OUTPUT**/

    StackOverflow.Demos.C
    StackOverflow.Demos.B
    StackOverflow.Demos.A
    System.Object

When you say GetType() , or equivalently this.GetType() , you get the actual type of the class. For example

class A
{
  public void Test()
  {
    Console.WriteLine(GetType().Name);
  }
}

class B : A
{
}

and later:

var myB = new B();
myB.Test();   // writes B to the console

I guess that's what polymorphism is all about.

If you want always A , no polymorphism, simply say:

class A
{
  public void Test()
  {
    Console.WriteLine(typeof(A).Name);
  }
}

class B : A
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM