【问题标题】:How do I get all the bottom types in an assembly?如何获得装配中的所有底部类型?
【发布时间】:2009-02-10 19:48:26
【问题描述】:

这是this one的姐妹问题

如果我有一个实例

System.Reflection.Assembly

我有以下型号:

class Person {}
class Student : Person {}
class Freshman : Student {}
class Employee : Person {}
class PersonList : ArrayList {}
class StudentList : PersonList {}

如何枚举程序集的类型以获取对 Employee、Freshman 和 StudentList 的引用?

我希望能够像上面的示例一样枚举任何给定程序集的所有底部类型。

感谢您的宝贵时间 :)

【问题讨论】:

    标签: c# .net reflection


    【解决方案1】:

    所以你想找到程序集中没有其他类型派生的所有类型,对吧?

    (为了可读性而重构。)

    var allTypes = assembly.GetTypes();
    var baseTypes = allTypes.Select(type => type.BaseType);
    var bottomTypes = allTypes.Except(baseTypes);
    

    (如果您想要 .NET 2.0 版本,请告诉我。会更痛苦。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      • 2020-04-20
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      相关资源
      最近更新 更多