获取继承来电类型名称基静态类静态、名称、类型

由网友(凹凸萌)分享简介:我有:class parent{public static string GetTypeName(){ /* here i want to get the caller's typeSo child.GetTypeName() should display "child" */} }...

我有:

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()
阅读全文

相关推荐

最新文章