Assembly.GetTypes Méthode

Définition

Obtient tous les types définis dans cet 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()

Retours

Type[]

Tableau qui contient tous les types définis dans cet assembly.

Implémente

Exceptions

L’assembly contient un ou plusieurs types qui ne peuvent pas être chargés. Le tableau retourné par la propriété Types de cette exception contient un objet Type pour chaque type qui a été chargé et null pour chaque type qui n’a pas pu être chargé, tandis que la propriété LoaderExceptions contient une exception pour chaque type qui n’a pas pu être chargé.

Exemples

L’exemple suivant affiche les paramètres d’une méthode sur un type dans l’assembly spécifié.

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

Remarques

Le tableau retourné inclut des types imbriqués et non publics. Pour récupérer uniquement les types publics, utilisez la GetExportedTypes méthode .

Si la GetTypes méthode est appelée sur un assembly et qu’un type de cet assembly dépend d’un type dans un assembly qui n’a pas été chargé (par exemple, s’il dérive d’un type dans le deuxième assembly), une ReflectionTypeLoadException valeur est levée. Par exemple, cela peut se produire si le premier assembly a été chargé avec les ReflectionOnlyLoad méthodes ou ReflectionOnlyLoadFrom et si le deuxième assembly n’a pas été chargé. Cela peut également se produire avec des assemblys chargés à l’aide des Load méthodes et LoadFile si le deuxième assembly ne peut pas être localisé lorsque la GetTypes méthode est appelée.

Notes

Si un type a été transféré à un autre assembly, il n’est pas inclus dans le tableau retourné. Pour plus d’informations sur le transfert de type, consultez Transfert de type dans le Common Language Runtime.

Pour récupérer une collection d’objets TypeInfo au lieu d’un tableau d’objets Type , utilisez la Assembly.DefinedTypes propriété .

S’applique à