我可以在Powershell中动态找到类的所有子类吗

我有一个抽象基类,我想要动态列出所有子类(当前已加载)的列表。但是,我还没有找到一个样本。我想要类似的东西:

 $subclasses = [AbstractRule].get-subclasses

任何提示表示赞赏。

评论
  • 劫走EXO
    劫走EXO 回复

    Enumerate all types in the current application domain, then use Type.IsSubclassOf() to find the one's you're interested in:

    $superType = [AbstractRule]
    $subTypes = [AppDomain]::Current.GetAssemblies().GetTypes() |Where {$_.IsSubclassOf($superType)}
    

    这还将包括使用PowerShell类定义的自定义派生类型(如果存在)

    You could also limit your search to just the assembly that contains [AbstractRule], if you suspect all the subclasses to be in the same:

    $subTypes = $superType.Assembly.GetTypes() |Where {$_.IsSubclassOf($superType)}