I’m trying to write a validation to check that an Object instance can be cast to a variable Type. I have a Type instance for the type of object they need to provide. But the Type can vary. This is basically what I want to do.
Object obj = new object(); Type typ = typeof(string); //just a sample, really typ is a variable if(obj is typ) //this is wrong 'is' does not work like this { //do something }
The type object itself has the IsSubClassOf, and IsInstanceOfType methods. But what I really want to check is if obj is either an instance of typ or any class derived from typ.
Seems like a simple question, but I can’t seem to figure it out.
How about this:
This tells you if that object can be casted to that certain type.