0

如何在基础中获取调用者类类型?

这是父母,这里我想打印子类型而不发送它

public abstract class Parent: ISomeInterface    {

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

孩子

public class Child : parent {

}

打印孩子类型:

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

谢谢

(我已经看过这个:在基静态类中获取继承的调用者类型名称,但我没有使用静态方法)

4

2 回答 2

2
Type typeOfMyChild = this.GetType();

由于调用时的多态性:

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

它应该打印Child

于 2011-09-25T09:22:45.347 回答
2

你不只是在寻找当前的类型吗?

    public void printChildType()
    {
         Type typeOfMyChild = GetType();
         MessageBox.Show(typeOfMyChild); 
    }
于 2011-09-25T09:24:28.363 回答