C #: check if a class is in an instance of a superclass instead of a subclass

I have a class that has a bunch of subclasses that inherit it. How can I check if an object is an instance of this superclass, and not any of the derived classes?

Example:

I have a Vehicle class, and it has several classes that inherit from it, such as car, motorcycle, bicycle, truck, etc.

Assuming this, how can I check if a vehicle’s object really belongs to the Vehicle class, and not to a car or bicycle? (Since in this case, a car and a bicycle are an example of the Vehicle class).

+5
source share
3 answers
if (theObject.GetType() == typeof(Vehicle))
{
   // it really a Vehicle instance
}
+12
source

Object.GetType(), .

Vehicle v = GetVehicle();

if(v.GetType() == typeof(Vehicle))
{
}
+2

:

bool isSuper = instance.GetType() == typeof(Vehicle);
+1
source

All Articles