-1

I've searched for a while, but I can't seem to find an answer to this problem. I need to figure out if the object I'm currently looking at IS a Subclass, or alternatively if it is inheriting from another class. I do NOT need to know if this class can inherit from another one, but rather ONLY to know if its a subclass. In the current context, I have no way to know what the parent class is, therefore I can't use:

  • typeof(Derived).IsSubclassOf(typeof(Base))
  • typeof(Base).IsAssignableFrom(typeof(Derived))
  • I cannot use is or as

Is there anything on the Type class or PropertyInfo class where I can check this?

  • 1
    You are aware that `typeof(T)` returns a `Type`, right? If you are asking where to get `IsSubclassOf` or `IsAssignableFrom` on a `Type`... well, `IsSubclassOf` and `IsAssignableFrom`. "In the current context, I don't know which classes are being used, therefore I can't use" - what context is this? - is [`object.GetType`](https://learn.microsoft.com/en-us/dotnet/api/system.object.gettype?view=netframework-4.8) what you want? - I'm voting to close. Addendum: perhaps `Type.IsInstanceOfType(Object)`. – Theraot Sep 13 '19 at 18:22
  • you can refer existing post in stack overflow: [a link](https://stackoverflow.com/questions/2742276/how-do-i-check-if-a-type-is-a-subtype-or-the-type-of-an-object) – Meow Sep 13 '19 at 18:27
  • 1
    @Theraot that is not what I am asking. As stated in my question, all I need to know is if the current class IS a subclass, nothing less, nothing more. I specifically stated that those examples don't work for me because I don't know which classes are being used, it's all being done with reflection and I have no way to know what the parent class is. That other post is specifically what I said its of no use to me... – Dan Espinosa Sep 13 '19 at 18:31
  • 3
    @DanEspinosa I bet all your classes are subclasses. They all inherit from `object`. I still don't get what you want, but this should help: [`Type.BaseType`](https://docs.microsoft.com/en-us/dotnet/api/system.type.basetype?redirectedfrom=MSDN&view=netframework-4.8#System_Type_BaseType). Returns "The Type from which the current Type directly inherits, or null if the current Type represents the Object class or an interface." Addendum: See [Find all parent types (both base classes and interfaces)](https://stackoverflow.com/questions/8868119/find-all-parent-types-both-base-classes-and-interfaces) – Theraot Sep 13 '19 at 18:36
  • @Theraot, yes I think this is the correct answer! This is what I need. Thank you! – Dan Espinosa Sep 13 '19 at 18:40
  • Possible duplicate of [How do I check if a type is a subtype OR the type of an object?](https://stackoverflow.com/questions/2742276/how-do-i-check-if-a-type-is-a-subtype-or-the-type-of-an-object) – Juan Salvador Portugal Sep 13 '19 at 19:38
  • @JuanSalvadorPortugal, as I already commented earlier, that is not the solution to my question. That is not what I am asking. There is already a solution given here that actually solves my problem. – Dan Espinosa Sep 13 '19 at 21:17

1 Answers1

2

Every class in C# is derived from Object, so it means to check if given class is subclass or not you can check Baseclass Property of type. snippet given below.

class Base {
}
class Derived : Base
{
}

now if you check

var isBase = typeof(Base).BaseType == typeof(Object) // true
isBase =  typeof(Derived).BaseType == typeof(Object) // false
Nauty
  • 166
  • 1
  • 13