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
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