开发者

C# get caller class type (Not in Static)

开发者 https://www.devze.com 2023-04-08 19:10 出处:网络
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

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); 
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号