Type from Scala reflection
Suppose that I have:
trait A
class B extends A
compiled into class files. Later I load those using reflection:
val a = Class forName "A"
val b = Class forName "B"
Could anyone tell me h开发者_StackOverflow中文版ow to check whether b
is the subtype of a
?
Use the isAssignableFrom
method in Class
:
a isAssignableFrom b
This returns true
if b
is a subclass/subinterface of a
or b == a
.
You can just call the getInterfaces
method on b
and iterate through the array to see if any of them equals a
.
精彩评论