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

Considering

public class Original {
  public static DoStuff() {
  }
}

public class Derived : Original {
}

      

when calling

Derived.DoStuff();

      

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

andDerived.DoStuff();

+3
c # static


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

+8


source to share


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



+1


source to share


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.

0


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics