获取继承来电类型名称基静态类 [英] Get inherited caller type name in base static class

查看:147
本文介绍了获取继承来电类型名称基静态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

class parent
{
    public static string GetTypeName()
    { 
        /* here i want to get the caller's type
        So child.GetTypeName() should display "child" */
    }            
}     

class child : parent { }

static void Main()
{
    Console.WriteLine(child.GetTypeName());
}

是否有可能以某种方式得到在基类中调用者的类型?

Is it possible somehow to get the caller's type in base class?

推荐答案

它,除非你通过调用者的方法(作为参数),或步行堆栈帧获得致电者是不可能的。

It is not possible unless you pass the caller to the method (as an argument) or walk the stack frame to get the caller.

编译器的替代品孩子打电话时通过孩子的静态方法键入。例如,这里的IL $ C $下调用 child.GetTypeName()

The compiler substitutes parent for child when calling parent's static methods through the child type. For example, here's the IL code for a call to child.GetTypeName():

IL_0002:  call   class [mscorlib]System.Type Tests.Program/parent::GetTypeName()

这篇关于获取继承来电类型名称基静态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆