PDA

View Full Version : Another newbie question


john.hui
03-07-2007, 06:28 PM
Hello Spring Team,

I am trying to perform something similar to the following code in VB.Net but using Expression


TypeOf anObject Is aType


I have tried using the following expression.


anObject.GetType() == T(aType)


However, the above will not work if anObject is an instance of a type derived from aType. How then do we achieve the goal of checking for subtypes?

Thanks.

John Hui

rolandz
03-08-2007, 11:53 AM
TypeOf anObject Is aType
I have tried using the following expression.


anObject.GetType() == T(aType)
However, the above will not work if anObject is an instance of a type derived from aType. How then do we achieve the goal of checking for subtypes?


In C# I would do it this way:

aType.IsAssignableFrom(anObject.GetType())

I am not very familiar in expressions but there must be very similar way...

Aleks Seovic
03-08-2007, 01:39 PM
Actually, SpEL supports "is" operator as well, so you can just write:


anObject.GetType() is T(aType)


Under the hood it will call aType.IsAssignableFrom(anObject.GetType()), as Roland suggested (which you could also do, btw, but "is" operator allows for a cleaner expression.

- Aleks

john.hui
03-08-2007, 07:57 PM
Aleks, Roland,

Thanks alot. I'll test it out once I come round to it.

John