如何获得装配中的所有底部类型?

时间:2023-01-14 14:28:50

This is sister question to this one

这是对这个问题的姐妹问题

If I have an instance of

如果我有一个实例

System.Reflection.Assembly

and I have the following model:

我有以下型号:

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

How can I enumerate the assembly's types to get a reference to Employee, Freshman, and StudentList?

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

I'd like to be able to enumerate all the bottom types for any given assembly like the example above.

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

Thanks for your time :)

谢谢你的时间 :)

1 个解决方案

#1


So you want to find all the types which no other type in the assembly derives from, right?

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

(Refactored for readability.)

(为了便于阅读而重新考虑。)

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

(Let me know if you want a .NET 2.0 version. It'll be a bit more painful.)

(如果你想要一个.NET 2.0版本,请告诉我。它会有点痛苦。)

#1


So you want to find all the types which no other type in the assembly derives from, right?

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

(Refactored for readability.)

(为了便于阅读而重新考虑。)

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

(Let me know if you want a .NET 2.0 version. It'll be a bit more painful.)

(如果你想要一个.NET 2.0版本,请告诉我。它会有点痛苦。)