Assembly.GetTypes 方法

定义

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

public:
 virtual cli::array <Type ^> ^ GetTypes();
public virtual Type[] GetTypes ();
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.
array<ParameterInfo^>^ Params = Method->GetParameters();
// Display information about method parameters.
// Param = sParam1
//   Type = System::String
//   Position = 0
//   Optional=False
for each ( ParameterInfo^ Param in Params )
{
   Console::WriteLine( "Param= {0}", Param->Name );
   Console::WriteLine( "  Type= {0}", Param->ParameterType );
   Console::WriteLine( "  Position= {0}", Param->Position );
   Console::WriteLine( "  Optional= {0}", Param->IsOptional );
}
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则会引发 。 例如,如果使用 或 ReflectionOnlyLoadFrom 方法加载ReflectionOnlyLoad第一个程序集,但未加载第二个程序集,则可能会发生这种情况。 如果在调用 方法时无法找到第二个程序集,则使用 LoadLoadFile 方法加载的 GetTypes 程序集也会发生这种情况。

注意

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

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

适用于