[英]C# get caller class type (Not in Static)
如何在基中获取调用方类类型?
这是父母,在这里我要打印子类型而不发送它
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();
谢谢
(我已经看到了这一点: 在基静态类中获取继承的调用方类型名称,但是我没有使用任何静态方法)
Type typeOfMyChild = this.GetType();
由于调用时具有多态性:
Child child = new Child();
child.printChildType();
它应该打印Child
。
您不只是在寻找当前类型吗?
public void printChildType()
{
Type typeOfMyChild = GetType();
MessageBox.Show(typeOfMyChild);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.