0

I have many classes.

public abstract class ClassA {}

public class ClassB : ClassA {}

public class ClassC : ClassA {}

public class ClassD : ClassB {}

public class ClassE : ClassB {}

public class ClassF : ClassC {}

public class ClassG : ClassC {}

I have an object that is typed as ClassA. I want to check if this object is an instance of ClassB or not, when it is actually an instance of D, E, F or G. Basically test if it is B, D or E, by checking if it inherits B or is B.

if (obj.GetType == typeof(ClassB)) returns false when ClassD or ClassE.

What should I do in that situation without explicitly checking for every single type that inherits from B?

Nico
  • 12,493
  • 5
  • 42
  • 62
Kaito Kid
  • 983
  • 4
  • 15
  • 34
  • What have you tried already? There are plenty of examples on Stack Overflow showing how to test inheritance relationships in C#. You should use those examples to solve your problem, instead of asking Stack Overflow to write your code for you. Also, when you write "generic", do you really mean "generic", as in a C# generic type? Or are you just misusing the term, and really mean "general"? – Peter Duniho Jul 11 '17 at 00:06
  • 3
    Is the answer [`is`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/is)? – Jeroen Mostert Jul 11 '17 at 00:07

0 Answers0