mardi 23 mai 2017

How to get correct caller type inside the inherited static method in C#

Look at this code:

public class A
{
    public static void Foo() 
    {
       // How to get typeof(B) here if Foo called by using class B? 
    }
}

public class B : A
{
}

...

static class Program
{
    static void Main()
    {
        B.Foo();
    }
}

Here I have two non-static classes, but I have one static method inside the parent class. My question is how to get the type of class which used to call this static method inside this method? I can't use keyword this, because I do not create any objects here. I have tried already: MethodBase.GetCurrentMethod().DeclaringType and MethodBase.GetCurrentMethod().ReflectedType, but they both return me the typeof(A), but I need to get the typeof(B).





Aucun commentaire:

Enregistrer un commentaire