Assembly.GetTypes Assembly.GetTypes Assembly.GetTypes Assembly.GetTypes Method

定义

获取此程序集中定义的类型。Gets the types defined in this assembly.

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[]

一个数组,包含此程序集中定义的所有类型。An array that contains all the types that are defined in this assembly.

实现

异常

该程序集包含一个或多个无法加载的类型。The assembly contains one or more types that cannot be loaded. 此异常的 Types 属性返回的数组针对已加载的每个类型包含一个 Type 对象,针对无法加载的每个类型包含一个 null,而 LoaderExceptions 属性针对无法加载的每个类型包含一个异常。The array returned by the Types property of this exception contains a Type object for each type that was loaded and null for each type that could not be loaded, while the LoaderExceptions property contains an exception for each type that could not be loaded.

示例

下面的示例显示指定程序集中某一类型的方法的参数。The following example displays parameters of one method on a type in the specified assembly.

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 

注解

返回的数组包括嵌套类型。The returned array includes nested types.

如果对程序集调用ReflectionTypeLoadException 方法,并且该程序集中的类型依赖于尚未加载的程序集中的类型(例如,如果它是从第二个程序集中的类型派生的),则会引发。GetTypesIf the GetTypes method is called on an assembly and a type in that assembly is dependent on a type in an assembly that has not been loaded (for example, if it derives from a type in the second assembly), a ReflectionTypeLoadException is thrown. 例如,如果第一个程序集是用ReflectionOnlyLoadReflectionOnlyLoadFrom方法加载的,并且第二个程序集未加载,则可能会发生这种情况。For example, this can happen if the first assembly was loaded with the ReflectionOnlyLoad or ReflectionOnlyLoadFrom methods, and the second assembly was not loaded. 如果在调用Load GetTypes方法时找不到第二LoadFile个程序集,则使用和方法加载的程序集也会发生这种情况。It can also happen with assemblies loaded using the Load and LoadFile methods if the second assembly cannot be located when the GetTypes method is called.

备注

如果类型已转发给另一个程序集,则它不会包含在返回的数组中。If a type has been forwarded to another assembly, it is not included in the returned array. 有关类型转发的信息,请参阅公共语言运行时中的类型转发For information on type forwarding, see Type Forwarding in the Common Language Runtime.

若要检索TypeInfo对象的集合而不是Type对象的数组,请使用Assembly.DefinedTypes属性。To retrieve a collection of TypeInfo objects instead of an array of Type objects, use the Assembly.DefinedTypes property.

适用于