C# 函数接受 Base Class Name/Type 作为参数和 returns 其所有 children 的名称

C# Function which accepts Base Class Name/Type as parameter and returns name of all its children

我需要编写一个函数,它在 C# 中接受基本 class 名称和所有 children 的 return 名称。 众所周知,在命名空间中,我们有几个基础 class,每个都有自己的 children。上述功能(将位于工具中)的目的是从用户那里获取一个基本 class 名称,然后是其所有 children 的 return 名称。 (我们可以使用结果来创建批处理文件) 我假设我需要使用反射和泛型但不确定如何使用。你能帮我一下吗?

如果我将基 class 的名称作为字符串传递,函数会是什么样子? 以及如果我将基 class 名称作为通用类型传递应该如何?

评论后提问的版本:

根据答案中建议的页面,我尝试了以下功能,但它 return对 kids 和 blahKids 都无效

 private object GetKidsOfBaseClass(string baseClass)
    {
        Type baseType = Type.GetType($"{baseClass}");
        var blahKids = AppDomain.CurrentDomain.GetAssemblies().SelectMany(ass => ass.GetTypes())
            .Where(p => p.IsSubclassOf(baseType));
        var kids = Assembly.GetAssembly(baseType).GetTypes().Where(t => t.IsSubclassOf(baseType));
        return kids;
    }

根据@madreflection 的建议第二版: 以下代码对我有用:

    private object GetKidsOfBaseClass(Type baseType)
    {
        var blahKids = 
        AppDomain.CurrentDomain.GetAssemblies().SelectMany(ass => ass.GetTypes())
            .Where(p => p.IsSubclassOf(baseType)).ToList();

        return blahKids;
    }

这是基于我的问题中添加的评论的解决方案:

private object GetKidsOfBaseClass(Type baseType)
{
    var blahKids = 
    AppDomain.CurrentDomain.GetAssemblies().SelectMany(ass => ass.GetTypes())
        .Where(p => p.IsSubclassOf(baseType)).ToList();

    return blahKids;
}