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

Definición

Obtiene los tipos definidos en este ensamblado.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()

Devoluciones

Type[]

Matriz que contiene todos los tipos que están definidos en este ensamblado.An array that contains all the types that are defined in this assembly.

Implementaciones

Excepciones

El ensamblado contiene uno o más tipos que no se pueden cargar.The assembly contains one or more types that cannot be loaded. La matriz devuelta por la propiedad Types de esta excepción contiene un objeto Type para cada tipo que se cargó y null para cada tipo que no se pudo cargar, mientras que la propiedad LoaderExceptions contiene una excepción para cada tipo que no se pudo cargar.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.

Ejemplos

El ejemplo siguiente muestra los parámetros de un método en un tipo en el ensamblado especificado.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 

Comentarios

La matriz devuelta incluye tipos anidados.The returned array includes nested types.

Si el GetTypes método se llama en un ensamblado y un tipo en que el ensamblado depende de un tipo en un ensamblado que no se ha cargado (por ejemplo, si se deriva de un tipo del segundo ensamblado), un ReflectionTypeLoadException se produce.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. Por ejemplo, esto puede ocurrir si el primer ensamblado se cargó con la ReflectionOnlyLoad o ReflectionOnlyLoadFrom métodos y el segundo ensamblado no se ha cargado.For example, this can happen if the first assembly was loaded with the ReflectionOnlyLoad or ReflectionOnlyLoadFrom methods, and the second assembly was not loaded. También puede ocurrir con los ensamblados cargados mediante el Load y LoadFile métodos si el segundo ensamblado no puede encontrar cuando el GetTypes se llama al método.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.

Nota

Si un tipo se ha reenviado a otro ensamblado, no se incluye en la matriz devuelta.If a type has been forwarded to another assembly, it is not included in the returned array. Para obtener información sobre el reenvío de tipos, vea reenvío de tipos en Common Language Runtime.For information on type forwarding, see Type Forwarding in the Common Language Runtime.

Para recuperar una colección de TypeInfo objetos en lugar de una matriz de Type objetos, utilice el Assembly.DefinedTypes propiedad.To retrieve a collection of TypeInfo objects instead of an array of Type objects, use the Assembly.DefinedTypes property.

Se aplica a