获取实现某个抽象类的所有类 [英] Get all classes that implement a certain abstract class

查看:44
本文介绍了获取实现某个抽象类的所有类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取实现某个抽象类的所有类.我正在尝试使用以下代码来做到这一点:

I'm trying to get all classes that implement a certain abstract class. I'm trying to do that with the following code:

var type = typeof(BaseViewComponent);
var types = Assembly
    .GetEntryAssembly()
    .GetReferencedAssemblies()
    .Select(Assembly.Load)
    .SelectMany(s => s.GetTypes())
    .Where(p => type.IsAssignableFrom(p));

但到目前为止,我只能自己获得抽象类.不是任何实现该基类的类.

But thus far I'm only able to get the abstract class it self. Not any class that implements that base class.

我需要改变什么才能获得实现这个抽象基类的所有类?

What do I have to change to get all the classes that implement this abstract base class?

推荐答案

using System.Reflection;
using Microsoft.Extensions.DependencyModel;

var asmNames = DependencyContext.Default.GetDefaultAssemblyNames();
var type = typeof(BaseViewComponent);

var allTypes = asmNames.Select(Assembly.Load)
    .SelectMany(t => t.GetTypes())
    .Where(p => p.GetTypeInfo().IsSubclassOf(type));

这篇关于获取实现某个抽象类的所有类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆