Objective-C中的后代枚举

2 inheritance runtime metaprogramming objective-c

是否有可能在objective-c中获取特定类的所有后代类的列表?

就像是:

  @interface A : NSObject
  @end

  @interface B : A
  @end

  @interface C : A
  @end

  NSArray *descendants = [A allDescendants];  // descendants = [B, C]
Run Code Online (Sandbox Code Playgroud)

Mat*_*her 5

我能想到的唯一方法是枚举运行时中的整个类列表(获取objc_getClassList)并测试每个类isKindOfClass:A.

这可能是唯一的解决方案,因为类不维护到其后代的链接(仅限于其超类).