0

I have a Type T, which I've loaded from an assembly via assembly.GetType("namespace.TypeName"), and an object o of an unknown class, received from a different place.

I need to check whether o is derived from T.

I've tried:

  • T.IsInstanceOfType(o), which does not work as expected, as explained here
  • o is T, which yields the compiler error 'T' is a 'field' but a 'type' was expected

Thanks for your help.

Community
  • 1
  • 1
mic_e
  • 5,594
  • 4
  • 34
  • 49

1 Answers1

2

Have a look at the IsAssignableFrom Method:

Type t = ...
Object o = ...

bool isODerivedFromT = (o == null) || t.IsAssignableFrom(o.GetType());
dtb
  • 213,145
  • 36
  • 401
  • 431