0

I have a class A with a static method, and a derived class B. You can call Foo(), declared in A, on both A and B:

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

Now inside Foo(), how can I find out on which type Foo() was called?

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), while I need to get the typeof(B).

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Ceridan
  • 2,376
  • 3
  • 23
  • 32
  • Have you tried using stacktrace? – Alfie Goodacre May 23 '17 at 09:32
  • 3
    You can't, basically. – Jon Skeet May 23 '17 at 09:32
  • @Alfie Goodacre, Stacktrace looks like a "dirty trick", so I did not try it yet. – Ceridan May 23 '17 at 09:35
  • @Jon Skeet, thank you! – Ceridan May 23 '17 at 09:36
  • The stack trace wouldn't make any difference, as despite the wording of the question, this isn't about the *caller* (which is `Program.Main`), it's about the target class of the static method invocation. – Jon Skeet May 23 '17 at 09:39
  • Can you explain _why_ you need to find out on which type a static method was called? – CodeCaster May 23 '17 at 09:41
  • @CodeCaster, I have a lot of "B" classes inherited from the common parent A. B classes have some attributes and my static method should analyze this attributes using reflection for each B class and return some info. Yes, I can make a parameter in the static method and use it for example like this: A.Foo(typeof(B)); A.Foo(typeof(C)); and so on, but B.Foo() is easier to read and write. This is not a big problem in my code. I asked this question to understand the situation in general. – Ceridan May 23 '17 at 09:49
  • Jon Skeet and CodeCaster helped me to understand this situation and the answer to my question is simple: you can't. We could close this topic. What should I do: post an answer to the topic by myself or just leave it? – Ceridan May 23 '17 at 10:00
  • I'm trying to find the relevant part of the C# spec, but can't find it. The problem is that `B.Foo()` gets compiled to `A.Foo()`, but I can't find any source for why that happens. So feel free to post a self-answer stating you can't do this. – CodeCaster May 23 '17 at 10:03
  • Check this http://rextester.com/EYEHF53987 – Prashant-Systematix May 23 '17 at 10:19

2 Answers2

0

I'm not sure exactly what you want to achieve, and agree with the comments that perhaps you can approach this a different way. Saying this, could you do what you need with a generic base class?

public class AB<T>
{
    public static void Foo()
    {
        Console.WriteLine(typeof(T));
    }
}

public class A : AB<A> { }

public class B : AB<B> { }

A.Foo(); // A
B.Foo(); // B
TVOHM
  • 2,740
  • 1
  • 19
  • 29
  • So create a wrapper class just for pointing out on which type you want to call a static method? Note that static methods can't implement interfaces, so you can't call `T.Foo()` inside your `Foo()`. – CodeCaster May 23 '17 at 09:45
  • Adding a Type parameter is much cleaner than enforcing a generic class. – Tanveer Badar Apr 01 '18 at 15:20
0

There are no way to obtain typeof(B) inside of static method Foo() in this case. The possible solution is to use parameters in Foo method.

Thanks to @Jon Skeet and @CodeCaster.

Ceridan
  • 2,376
  • 3
  • 23
  • 32