In C# what is the equivalent of “is” keyword but using Type objects

后端 未结 3 1423
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-06 06:00

An easy question I guess, but in the documentation of the Type class they only talk of interfaces on the GetInterfaces method.

i.e. typeof(ChildClass).XXX(typeof(Par

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-06 06:32

    It depends on what you need; IsAssignableFrom, perhaps:

    bool stringIsObj = typeof(object).IsAssignableFrom(typeof(string));
    

    or IsSubclassOf:

    bool stringIsObj = typeof(string).IsSubclassOf(typeof(object));
    

提交回复
热议问题