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 則會擲回 。 例如,如果第一個元件是使用 ReflectionOnlyLoadReflectionOnlyLoadFrom 方法載入,而且第二個元件未載入,就可能發生此情況。 如果呼叫 方法時 GetTypes 無法找到第二個元件,使用 和 LoadFile 方法載入 Load 的元件也可能會發生此情況。

注意

如果類型已轉送至另一個元件,則不會包含在傳回的陣列中。 如需類型轉送的資訊,請參閱 Common Language Runtime 中的類型轉送

若要擷取 物件的集合 TypeInfo ,而不是 物件的陣列 Type ,請使用 Assembly.DefinedTypes 屬性。

適用於