Get inherited caller type name in base static class

前端 未结 2 1464
暗喜
暗喜 2021-01-20 12:06

i have:

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


        
2条回答
  •  醉梦人生
    2021-01-20 12:34

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

    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()
    

提交回复
热议问题