In C# how can you find if an object is an instance of certain class but not any of that class’s superclasses?
“is” will return true even if the object is actually from a superclass.
typeof(SpecifiedClass) == obj.GetType()
You could compare the type of your object to the Type of the class that you are looking for:
class A { }
class B : A { }
A a = new A();
if(a.GetType() == typeof(A)) // returns true
{
}
A b = new B();
if(b.GetType() == typeof(A)) // returns false
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With