Comparing Type variables

J

Jeronimo Bertran

I have a questions regarding comparing types. Suppose I have the following
classes:


class class1
{
}

class derivedClass : class1
{
}


derivedClass myObject;

I now have a function that takes a Type as a parameter

bool TestType(object o, Type typeToTest)
{

// accomplish something like return (o is typeToTest)

return o is typeToTest; // This won't work because typeToTest is a
variable and not the actual type but I want to be able to check if o is
derived from the type supplied.

return o.GetType() == typeToTest; // This doesn't work either
because it only checks if the object is of the given type but not if it can
be casted to the given type.

}


bool result = TestType (myObject, typeof(class1));

Thanks

Jeronimo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top