tags:

views:

424

answers:

5

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.

A: 

If you have defined your own conversion you won't be able to use the is or as operators. Your best bet is probably to put the conversion code in a try/catch block and if it succeeds you know the conversion was ok and if not the types were incompatible.

sipwiz
try/catch for this? I don't think so. (but -1 is not mine!)
M. Jahedbozorgan
+6  A: 

How about this:


    MyObject myObject = new MyObject();
    Type type = myObject.GetType();

    if(typeof(YourBaseObject).IsAssignableFrom(type))
    {  
       //Do your casting.
       YourBaseObject baseobject = (YourBaseObject)myObject;
    }  


This tells you if that object can be casted to that certain type.

Hadi Eskandari
Yeah I found that later last night. Thanks though.
Eric Anastas
+3  A: 

Type.IsAssignableFrom

Simon Svensson
+4  A: 

I think you need to restate your conditions, because if obj is an instance of Derived, it will also be an instance of Base. And typ.IsIstanceOfType(obj) will return true.

class Base { }
class Derived : Base { }

object obj = new Derived();
Type typ = typeof(Base);

type.IsInstanceOfType(obj); // = true
type.IsAssignableFrom(obj.GetType()); // = true
Samuel
+2  A: 

If you are working with Instances, you should be going for Type.IsInstanceOfType

(Returns) true if the current Type is in the inheritance hierarchy of the object represented by o, or if the current Type is an interface that o supports. false if neither of these conditions is the case, or if o is nullNothingnullptra null reference (Nothing in Visual Basic), or if the current Type is an open generic type (that is, ContainsGenericParameters returns true). -- MSDN

        Base b = new Base();
        Derived d = new Derived();
        if (typeof(Base).IsInstanceOfType(b)) 
            Console.WriteLine("b can come in.");    // will be printed
        if (typeof(Base).IsInstanceOfType(d)) 
            Console.WriteLine("d can come in.");    // will be printed

If you are working with Type objects, then you should look at Type.IsAssignableFrom

(Returns) true if c and the current Type represent the same type, or if the current Type is in the inheritance hierarchy of c, or if the current Type is an interface that c implements, or if c is a generic type parameter and the current Type represents one of the constraints of c. false if none of these conditions are true, or if c is nullNothingnullptra null reference (Nothing in Visual Basic). -- MSDN

Gishu