I have three classes in dart:
class A {}
class B extends A{}
class C extends A{}
There is a way to get all subclasses from A?
Edit:
Thanks Alexandre Ardhuin your solution worked perfectly!
I'm learning the dart
i edited your code and put the recursive solution, take a look:
import 'dart:mirrors';
class A {}
class B extends A{}
class C extends A{}
class D {}
class E extends C {}
bool isParent(String parent,ClassMirror clazz){
var objectParentName = MirrorSystem.getName(clazz.superclass.simpleName);
if (objectParentName == "Object"){
return parent == "Object";
}
var result = parent == objectParentName;
if (!result){
return isParent(parent,clazz.superclass);
}
return result;
}
List<String> findAllSubclasses(String clazz){
var result = [];
final ms = currentMirrorSystem();
ms.isolate.rootLibrary.classes.forEach((s,c) {
if (isParent(clazz,c)){
result.add(MirrorSystem.getName(c.simpleName));
}
});
return result;
}
main(){
var result = findAllSubclasses('A');
print(result);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…