Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetType() returns subclass Type in superclass constructor

Tags:

c#

.net

.net-4.0

I have three classes: ClassA, ClassB, ClassC.

ClassC extends ClassB which in turn extends ClassA.

When I call GetType() from ClassA's constructor, .net is returning ClassC's type. I'm baffled because this is happening in code that has been working for a while and which I haven't touch in a while. Was there a hotfix to .net that changed the behavior of GetType()? I doubt that. My only other thought is that this has something to do with xUnit, the testing framework I'm using?

like image 201
SFun28 Avatar asked Aug 28 '26 07:08

SFun28


1 Answers

It's behaving exactly as it's supposed to. GetType() returns the actual type of the instance of the object, even if it's a subclass (and it has always behaved this way). typeof(ClassA) on the other hand would always give you the type for ClassA.

like image 53
Kirk Woll Avatar answered Aug 29 '26 20:08

Kirk Woll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!