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. 類型轉送上的資訊,請參閱Common Language Runtime 中的型別轉送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.

適用於