GetType () returns a subclass of Type in the constructor of the superclass

I have three classes: ClassA, ClassB, ClassC.

ClassCextends ClassB, which in turn extends ClassA.

When I call GetType () from the constructor ClassA, .net returns a type ClassC. I am confusing because this happens in code that worked for a while and which I did not touch at that time. Was there a fix for .net that changed the behavior of GetType ()? I doubt it. My only thought is that this has anything to do with xUnit, the testing platform used?

+5
source share
4 answers

, . GetType() , ( ). typeof(ClassA), , ClassA.

+12

, ClassA a, , , ClassC. , ClassA, :

- , :

ClassA a = new ClassC().

, ClassC : ClassB ClassB : ClassA .

GetType() , .

+2

GetType . . , - , , , .

,

typeof(TheContainingType)
MethodBase.GetCurrentMethod().DeclaringType
+2

As far as I remember, GetType () always returned the type of the instance, not the base class, regardless of where it is called.

+1
source

All Articles