문제

How do I get the caller class type in the base?

this is the parent, here I want to print the child type without sending it

public abstract class Parent: ISomeInterface    {

        public void printChildType()
        {
             Type typeOfMyChild = ?????;
             MessageBox.Show(typeOfMyChild); //how do I get Child typeOfMyChild
        }
}

the child

public class Child : parent {

}

pirnt the child type :

Child child = new Child();
child.printChildType();

Thanks

(I already saw this one: Get inherited caller type name in base static class but I am using none static methods)

도움이 되었습니까?

해결책

Type typeOfMyChild = this.GetType();

Thanks to polymorphism when you invoke:

Child child = new Child();
child.printChildType();

it should print Child.

다른 팁

Aren't you just looking for the current type ?

    public void printChildType()
    {
         Type typeOfMyChild = GetType();
         MessageBox.Show(typeOfMyChild); 
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top