Is it possible to determine the type when a static call is called by a derived type


public class Original {
  public static DoStuff() {

public class Derived : Original {


when calling



The perversity of the requirement aside, is it possible to DoStuff()

detect the class to which it was called?

i.e. is it possible in the implementation to DoStuff()

tell the difference between Original.DoStuff();



source to share

3 answers

Not in C #, no - the compiled IL refers directly to Original.DoStuff


(I just confirmed that VB seems to do the same for static calls, whereas in IRC there is a difference between VB and C # in the generated code when a virtual method is called with a "child" reference.)



If Derived

provides its own definition DoStuff

, it is Derived.DoStuff()

equivalent Original.DoStuff()

. Static methods / member variables are associated with a class, not with any instance (s).



As John said, this is not possible in C # because static methods have unique entry points. In any case, you are right, it is very strange that it will need to be detected.



All Articles