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

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

问题描述

我正在尝试获取实现特定抽象类的所有类。我正在尝试使用以下代码进行操作:

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天全站免登陆