System.Type usage
-
Every different class/struct/whatever returns a unique Type class with the GetType() function, right? Even when the object is cast into Object, it still returns the previous one, right? Now how do I see if that type is derived from some other thing? For example, if I have Object o and I need to see if it derives from ISomething interface. If System.Type is not for this and does soemthing completely different, what way could I do this?
-
Every different class/struct/whatever returns a unique Type class with the GetType() function, right? Even when the object is cast into Object, it still returns the previous one, right? Now how do I see if that type is derived from some other thing? For example, if I have Object o and I need to see if it derives from ISomething interface. If System.Type is not for this and does soemthing completely different, what way could I do this?
To test if an object implements an interface or derives from a type:
if( theObject is ISomething ) { }
orISomething something = theObject as ISomething;
In the lattersomething
will set to null iftheObject
doesn't support the interface Andres Manggini. Buenos Aires - Argentina. -
To test if an object implements an interface or derives from a type:
if( theObject is ISomething ) { }
orISomething something = theObject as ISomething;
In the lattersomething
will set to null iftheObject
doesn't support the interface Andres Manggini. Buenos Aires - Argentina. -
Every different class/struct/whatever returns a unique Type class with the GetType() function, right? Even when the object is cast into Object, it still returns the previous one, right? Now how do I see if that type is derived from some other thing? For example, if I have Object o and I need to see if it derives from ISomething interface. If System.Type is not for this and does soemthing completely different, what way could I do this?
As Andres pointed out you can use the is/as statements to determine whether an object can be cast to the specified type name. In this case you use the name of the type, not the Type object representing that type. So for this to work you have to be able to reference the typename at compile time. If the types are loaded at runtime then you can't use the above methods, instead if you have two Type objects, one from each of the two variables then you can use something like this.
bool IsBDerivedFromA(object a, object b)
{
Type ta = a.GetType();
Type tb = b.GetType();// NOTE: Returns false if a and b are of the
// same type, like two strings, two ints,
// two Foos, etc
return tb.IsSubclassOf(a);
}James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation
-
As Andres pointed out you can use the is/as statements to determine whether an object can be cast to the specified type name. In this case you use the name of the type, not the Type object representing that type. So for this to work you have to be able to reference the typename at compile time. If the types are loaded at runtime then you can't use the above methods, instead if you have two Type objects, one from each of the two variables then you can use something like this.
bool IsBDerivedFromA(object a, object b)
{
Type ta = a.GetType();
Type tb = b.GetType();// NOTE: Returns false if a and b are of the
// same type, like two strings, two ints,
// two Foos, etc
return tb.IsSubclassOf(a);
}James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation
-
I find
IsAssignableFrom()
better for most cases as it follows natural logic. :) I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02leppie wrote: IsAssignableFrom() better for most cases as it follows natural logic Yep, I like that one better. I suggested IsSubclassOf because that was the only method I could remember while my MSDN was being laggy. James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation
-
leppie wrote: IsAssignableFrom() better for most cases as it follows natural logic Yep, I like that one better. I suggested IsSubclassOf because that was the only method I could remember while my MSDN was being laggy. James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation