通过


Assembly.GetTypes 方法

定义

获取此程序集中定义的所有类型。

public:
 virtual cli::array <Type ^> ^ GetTypes();
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types might be removed")]
public virtual Type[] GetTypes();
public virtual Type[] GetTypes();
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types might be removed")>]
abstract member GetTypes : unit -> Type[]
override this.GetTypes : unit -> Type[]
abstract member GetTypes : unit -> Type[]
override this.GetTypes : unit -> Type[]
Public Overridable Function GetTypes () As Type()

返回

Type[]

一个数组,其中包含此程序集中定义的所有类型。

实现

属性

例外

程序集包含一个或多个无法加载的类型。 此异常属性返回 Types 的数组包含 Type 加载的每个类型的对象,以及 null 无法加载的每个类型的对象,而 LoaderExceptions 该属性包含无法加载的每个类型的异常。

示例

以下示例显示指定程序集中某一类型的一种方法的参数。

Assembly SampleAssembly;
SampleAssembly = Assembly.LoadFrom("c:\\Sample.Assembly.dll");
// Obtain a reference to a method known to exist in assembly.
MethodInfo Method = SampleAssembly.GetTypes()[0].GetMethod("Method1");
// Obtain a reference to the parameters collection of the MethodInfo instance.
ParameterInfo[] Params = Method.GetParameters();
// Display information about method parameters.
// Param = sParam1
//   Type = System.String
//   Position = 0
//   Optional=False
foreach (ParameterInfo Param in Params)
{
    Console.WriteLine("Param=" + Param.Name.ToString());
    Console.WriteLine("  Type=" + Param.ParameterType.ToString());
    Console.WriteLine("  Position=" + Param.Position.ToString());
    Console.WriteLine("  Optional=" + Param.IsOptional.ToString());
}
Dim SampleAssembly As [Assembly]
SampleAssembly = [Assembly].LoadFrom("c:\Sample.Assembly.dll")
' Obtain a reference to a method known to exist in assembly.
Dim Method As MethodInfo = SampleAssembly.GetTypes()(0).GetMethod("Method1")
' Obtain a reference to the parameters collection of the MethodInfo instance.
Dim Params As ParameterInfo() = Method.GetParameters()
' Display information about method parameters.
' Param = sParam1
'   Type = System.String
'   Position = 0
'   Optional=False
For Each Param As ParameterInfo In Params
    Console.WriteLine(("Param=" + Param.Name.ToString()))
    Console.WriteLine(("  Type=" + Param.ParameterType.ToString()))
    Console.WriteLine(("  Position=" + Param.Position.ToString()))
    Console.WriteLine(("  Optional=" + Param.IsOptional.ToString()))
Next

注解

返回的数组包括嵌套和非公共类型。 若要仅检索公共类型,请使用 GetExportedTypes 该方法。

GetTypes如果在程序集上调用该方法,并且该程序集中的类型依赖于尚未加载的程序集中的类型(例如,如果该方法派生自第二个程序集中的类型),则会引发一个ReflectionTypeLoadException。 例如,如果第一个程序集使用 ReflectionOnlyLoadReflectionOnlyLoadFrom 方法加载,并且第二个程序集未加载,则可能会发生这种情况。 如果调用该方法时GetTypes无法找到第二个程序集,则使用和LoadFile方法加载Load的程序集也会发生这种情况。

注释

如果类型已转发到另一个程序集,则它不包括在返回的数组中。 有关类型转发的信息,请参阅 公共语言运行时中的类型转发

若要检索对象集合 TypeInfo 而不是对象数组 Type ,请使用该 Assembly.DefinedTypes 属性。

适用于