如何找到属于基类的直接后裔类型? [英] How to find types that are direct descendants of a base class?

查看:136
本文介绍了如何找到属于基类的直接后裔类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获得所有类型的继承一些基类,但只有第一个后代的组装。举例来说,如果我有:

I need to get all types of an assembly that inherits some base class but only the first descendants. For instance if I have:

class Base
{

}

class FirstClass : Base
{

}

class SecondClass : FirstClass
{

}

现在

var directOnes = assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(Base)));



应该只返回的Firstclass ,而不是二等。有没有办法找出?

should return only FirstClass and not SecondClass. Is there a way to find out?

推荐答案

而不是的IsSubclassOf()您可以使用 Type.BaseType

Instead of IsSubclassOf() you can use Type.BaseType

var directOnes = assembly.GetTypes().Where(t => t.BaseType == (typeof(Base)));



(FYI:我不认为有一种方法找到的接口一个类型直接实现。)

(FYI: I don't think there's a way to find the interfaces that a type directly implements.)

这篇关于如何找到属于基类的直接后裔类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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