Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取程序集中的所有基类型?_C#_.net_Reflection - Fatal编程技术网

C# 如何获取程序集中的所有基类型?

C# 如何获取程序集中的所有基类型?,c#,.net,reflection,C#,.net,Reflection,所以如果我有一个 System.Reflection.Assembly 我有以下模型: class Person {} class Student : Person {} class Freshman : Student {} class Employee : Person {} class PersonList : ArrayList {} class StudentList : PersonList {} 如何枚举程序集的类型以仅获取对Person和PersonList类型的引用 需要

所以如果我有一个

System.Reflection.Assembly 
我有以下模型:

class Person {}
class Student : Person {}
class Freshman : Student {}
class Employee : Person {}
class PersonList : ArrayList {}
class StudentList : PersonList {}
如何枚举程序集的类型以仅获取对Person和PersonList类型的引用

需要明确的是:我不想在此查找过程中显式指定Person或PersonList类型。Person和PersonList只是在本例中讨论的程序集中定义的根类型。我正在寻找一种通用方法来枚举给定程序集的所有根类型

谢谢您的时间:)

怎么样:

var rootTypes = from type in assembly.GetTypes()
                where type.IsClass && type.BaseType == typeof(object)
                select type;
??或以非LINQ术语:

foreach (Type type in assembly.GetTypes())
{
    if (type.IsClass && type.BaseType == typeof(object))
    {
        Console.WriteLine(type);
    }
}
编辑:不,这不会发现个人列表。您需要更清楚地了解“根”的定义。你的意思是“基类型不在同一程序集中的任何类型”?如果是:

那么:

var rootTypes = from type in assembly.GetTypes()
                where type.IsClass && type.BaseType == typeof(object)
                select type;
??或以非LINQ术语:

foreach (Type type in assembly.GetTypes())
{
    if (type.IsClass && type.BaseType == typeof(object))
    {
        Console.WriteLine(type);
    }
}
编辑:不,这不会发现个人列表。您需要更清楚地了解“根”的定义。你的意思是“基类型不在同一程序集中的任何类型”?如果是:


如果程序集将PersonList作为从ArrayList继承的根类型,这是否仍然有效?很抱歉,不清楚。我不知道该怎么问这个问题。答案的后半部分看起来很棒。我会努力的。谢谢。如果程序集将PersonList作为从ArrayList继承的根类型,这是否仍然有效?很抱歉,不清楚。我不知道该怎么问这个问题。答案的后半部分看起来很棒。我会努力的。谢谢