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.

如果GetTypes程序集和类型上调用方法,该程序集中依赖于尚未加载 (例如,如果它从第二个集中的类型派生),程序集中类型ReflectionTypeLoadException引发。If 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. 它也会与程序集使用加载LoadLoadFile方法,如果第二个程序集不能定位时GetTypes调用方法。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.

适用于