Get Caller derived-class when calling a base-class static method

大城市里の小女人 提交于 2019-11-28 01:27:18

问题


I was wondering if it's possible (even via reflection et similia) to get the caller derived-class inside of a called base-class static method.

For example, I've a base-class with a static method defined:

public MyBaseClass {
    public static void MyBaseClassStaticMethod() { /** ... **/ }
}

and a derived-from-it class:

public MyDerivedClass : MyBaseClass { }

then I call:

MyDerivedClass.MyBaseClassStaticMethod()

Is it possibile, inside of method MyBaseClassStaticMethod, to know which is the caller derived type?
(i.e. MyDerivedClass)

I just need a string...


回答1:


No, this is not possible - by no means. static methods are not polymorphal and as such this information simply doesn't exist.
Consider redesigning your code.

Update:

Upon compilation, the compiler replaces MyDerivedClass with the class the static method is actually declared on, in your case MyBaseClass.
So even in the IL you don't see MyDerivedClass. The information exists only in your source code. It doesn't exist in your compiled assembly.




回答2:


Generics in following way can be used to solve your scenario

public class BaseClass<TDerived> where TDerived : BaseClass<TDerived>
{
    public static void LogCallerType()
    {
        Console.WriteLine(typeof(TDerived).Name);
    }
}

public class FooClass : BaseClass<FooClass> { }

public class BooClass : BaseClass<BooClass> { }

class Program
{
    static void Main(string[] args)
    {
        FooClass.LogCallerType();
        BooClass.LogCallerType();
    }
}

This will in turn output the following

1. FooClass
2. BooClass



回答3:


A static method is statically bound to a certain class and does not really participate in the inheritance-chain. Thus it does not exist in the derived class. The static method therefor does not know that it was actually used in the derived class.

You can however - through a compiler-trick - access the static member from your derived class. As of this post on MSDN-forum a static member-access from a derived class is translated to a call from the base-class containing the static member. So MyDerivedClass.MyBaseClassStaticMethod is translated to MyBaseClass.MyBaseClassStaticMethod. Thus MethodBase.GetCurrentMethod().DeclaringType will allways return MyBaseClass.

So in short: no, it´s not possible to get the derived type from a static member.




回答4:


First of all, the static method will not have access to the instance that is calling it. A static method is different from a normal class method in that it does not have access the 'this' reference to a class instance.

If you passed 'this' as a parameter to the static method, then you can try casting it as follows. Assume you have a number of derived class which you want to test for.

public static void MyBaseClassStaticMethod(MyBaseClass callingInstance)
{
    MyDerivedClass myDerivedClass = callingInstance as MyDerivedClass;
    MyDerivedClass2 myDerivedClass2 = callingInstance as MyDerivedClass2;
    MyDerivedClass3 myDerivedClass3 = callingInstance as MyDefivedClass3;
    ...

    // test for which derived class is calling
    if (myDerivedClass != null)
        ...
    else if (myDerivedClass2 != null)
        ...

    ...
}


来源:https://stackoverflow.com/questions/16301914/get-caller-derived-class-when-calling-a-base-class-static-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!