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

Re: Comparing Type variables by Peter

Peter
Fri Mar 14 02:21:57 CDT 2008

On Thu, 13 Mar 2008 23:55:36 -0700, Jeronimo Bertran
<jeronimo.bertran@newsgroup.nospam> wrote:

> [...]
> 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.

See Type.IsInstanceOf()
http://msdn2.microsoft.com/en-us/library/system.type.isinstanceoftype.aspx

Pete

Re: Comparing Type variables by Jeronimo

Jeronimo
Fri Mar 14 11:59:51 CDT 2008

"Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote in
news:op.t7zv2van8jd0ej@petes-computer.local:

> http://msdn2.microsoft.com/en-us/library/system.type.isinstanceoftype.a
> spx

Great .. thanks Pete

Re: Comparing Type variables by Jeronimo

Jeronimo
Fri Mar 14 11:59:59 CDT 2008

"Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote in
news:op.t7zv2van8jd0ej@petes-computer.local:

> http://msdn2.microsoft.com/en-us/library/system.type.isinstanceoftype.a
> spx

Great .. thanks Pete