Assembly.GetType Metodo

Definizione

Ottiene l'oggetto Type che rappresenta il tipo specificato.

Overload

Nome Descrizione
GetType(String, Boolean, Boolean)

Ottiene l'oggetto Type con il nome specificato nell'istanza dell'assembly, con le opzioni di ignorare il caso e di generare un'eccezione se il tipo non viene trovato.

GetType(String, Boolean)

Ottiene l'oggetto Type con il nome specificato nell'istanza dell'assembly e, facoltativamente, genera un'eccezione se il tipo non viene trovato.

GetType()
GetType(String)

Ottiene l'oggetto Type con il nome specificato nell'istanza dell'assembly.

GetType(String, Boolean, Boolean)

Ottiene l'oggetto Type con il nome specificato nell'istanza dell'assembly, con le opzioni di ignorare il caso e di generare un'eccezione se il tipo non viene trovato.

public:
 virtual Type ^ GetType(System::String ^ name, bool throwOnError, bool ignoreCase);
public virtual Type GetType(string name, bool throwOnError, bool ignoreCase);
public Type GetType(string name, bool throwOnError, bool ignoreCase);
override this.GetType : string * bool * bool -> Type
Public Overridable Function GetType (name As String, throwOnError As Boolean, ignoreCase As Boolean) As Type
Public Function GetType (name As String, throwOnError As Boolean, ignoreCase As Boolean) As Type

Parametri

name
String

Nome completo del tipo.

throwOnError
Boolean

true per generare un'eccezione se il tipo non viene trovato; false per restituire null.

ignoreCase
Boolean

true per ignorare la distinzione tra maiuscole e minuscole del nome del tipo; in caso contrario, false.

Valori restituiti

Oggetto che rappresenta la classe specificata.

Implementazioni

Eccezioni

name non è valido.

oppure

La lunghezza di name supera i 1024 caratteri.

name è null.

throwOnError è truee non è possibile trovare il tipo .

name richiede un assembly dipendente che non è stato trovato.

name richiede un assembly dipendente trovato ma che non è stato possibile caricare.

oppure

L'assembly corrente è stato caricato nel contesto di sola reflection e name richiede un assembly dipendente che non è stato precaricato.

name richiede un assembly dipendente, ma il file non è un assembly valido per il runtime attualmente caricato.

Commenti

Questo metodo esegue la ricerca solo nell'istanza dell'assembly corrente. Il name parametro include lo spazio dei nomi ma non l'assembly. Per cercare un tipo in altri assembly, usare l'overload del Type.GetType(String) metodo, che può facoltativamente includere un nome visualizzato dell'assembly come parte del nome del tipo.

In .NET Core/.NET 5+, se sono presenti parametri di tipo generico qualificati per assembly nella stringa del nome del tipo, tali riferimenti all'assembly verranno caricati dal AssemblyLoadContext del metodo che ha chiamato Assembly.GetType o dal contesto AssemblyLoadContext.CurrentContextualReflectionContext se impostato.

Annotazioni

Se il tipo è stato inoltrato a un altro assembly, viene comunque restituito da questo metodo. Per informazioni sull'inoltro dei tipi, vedere Inoltro dei tipi in Common Language Runtime.

Il throwOnError parametro influisce solo su ciò che accade quando il tipo non viene trovato. Non influisce su altre eccezioni che potrebbero essere generate. In particolare, se il tipo viene trovato ma non può essere caricato, TypeLoadException può essere generato anche se throwOnError è false.

Si applica a

GetType(String, Boolean)

Ottiene l'oggetto Type con il nome specificato nell'istanza dell'assembly e, facoltativamente, genera un'eccezione se il tipo non viene trovato.

public:
 virtual Type ^ GetType(System::String ^ name, bool throwOnError);
public virtual Type GetType(string name, bool throwOnError);
override this.GetType : string * bool -> Type
Public Overridable Function GetType (name As String, throwOnError As Boolean) As Type

Parametri

name
String

Nome completo del tipo.

throwOnError
Boolean

true per generare un'eccezione se il tipo non viene trovato; false per restituire null.

Valori restituiti

Oggetto che rappresenta la classe specificata.

Implementazioni

Eccezioni

name non è valido.

oppure

La lunghezza di name supera i 1024 caratteri.

name è null.

throwOnError è truee non è possibile trovare il tipo .

name richiede un assembly dipendente che non è stato trovato.

name richiede un assembly dipendente trovato ma che non è stato possibile caricare.

oppure

L'assembly corrente è stato caricato nel contesto di sola reflection e name richiede un assembly dipendente che non è stato precaricato.

name richiede un assembly dipendente, ma il file non è un assembly valido per il runtime attualmente caricato.

Commenti

Questo metodo esegue la ricerca solo nell'istanza dell'assembly corrente. Il name parametro include lo spazio dei nomi ma non l'assembly. Per cercare un tipo in altri assembly, usare l'overload del Type.GetType(String) metodo, che può facoltativamente includere un nome visualizzato dell'assembly come parte del nome del tipo.

In .NET Core/.NET 5+, se sono presenti parametri di tipo generico qualificati per assembly nella stringa del nome del tipo, tali riferimenti all'assembly verranno caricati dal AssemblyLoadContext del metodo che ha chiamato Assembly.GetType o dal contesto AssemblyLoadContext.CurrentContextualReflectionContext se impostato.

Annotazioni

Se il tipo è stato inoltrato a un altro assembly, viene comunque restituito da questo metodo. Per informazioni sull'inoltro dei tipi, vedere Inoltro dei tipi in Common Language Runtime.

Il throwOnError parametro influisce solo su ciò che accade quando il tipo non viene trovato. Non influisce su altre eccezioni che potrebbero essere generate. In particolare, se il tipo viene trovato ma non può essere caricato, TypeLoadException può essere generato anche se throwOnError è false.

Si applica a

GetType()

public:
 virtual Type ^ GetType();
public Type GetType();
override this.GetType : unit -> Type
Public Function GetType () As Type

Valori restituiti

Implementazioni

Si applica a

GetType(String)

Ottiene l'oggetto Type con il nome specificato nell'istanza dell'assembly.

public:
 virtual Type ^ GetType(System::String ^ name);
public virtual Type GetType(string name);
override this.GetType : string -> Type
Public Overridable Function GetType (name As String) As Type

Parametri

name
String

Nome completo del tipo.

Valori restituiti

Oggetto che rappresenta la classe specificata o null se la classe non viene trovata.

Implementazioni

Eccezioni

name non è valido.

name è null.

name richiede un assembly dipendente che non è stato trovato.

name richiede un assembly dipendente trovato ma che non è stato possibile caricare.

oppure

L'assembly corrente è stato caricato nel contesto di sola reflection e name richiede un assembly dipendente che non è stato precaricato.

name richiede un assembly dipendente, ma il file non è un assembly valido per il runtime attualmente caricato.

Esempio

Nell'esempio seguente viene definita una classe astratta MeansOfTransportation nello spazio dei Transportation nomi . Chiama il GetType(String) metodo per recuperare il relativo Type oggetto, chiama il Type.GetProperties metodo per ottenere una matrice di PropertyInfo oggetti che rappresentano le proprietà del tipo e quindi visualizza informazioni sulle proprietà astratte del tipo. Si noti che la chiamata al GetType(String) metodo usa il nome completo del tipo, ovvero il relativo spazio dei nomi insieme al nome del tipo.

using System;
using System.Reflection;

public class Example
{
    public static void Main()
    {
        Assembly assem = typeof(Example).Assembly;
        Type t = assem.GetType("Transportation.MeansOfTransportation");
        if (t != null)
        {
            Console.WriteLine($"Virtual properties in type {t.FullName}:");
            PropertyInfo[] props = t.GetProperties();
            int nVirtual = 0;
            for (int ctr = 0; ctr < props.Length; ctr++)
            {
                if (props[ctr].GetMethod.IsVirtual)
                {
                    Console.WriteLine($"   {props[ctr].Name} (type {props[ctr].PropertyType.FullName})");
                    nVirtual++;
                }
            }

            if (nVirtual == 0)
                Console.WriteLine("   No virtual properties");
        }
    }
}

namespace Transportation
{
    public abstract class MeansOfTransportation
    {
        abstract public bool HasWheels { get; set; }
        abstract public int Wheels { get; set; }
        abstract public bool ConsumesFuel { get; set; }
        abstract public bool Living { get; set; }
    }
}
// The example displays the following output:
//    Virtual properties in type Transportation.MeansOfTransportation:
//       HasWheels (type System.Boolean)
//       Wheels (type System.Int32)
//       ConsumesFuel (type System.Boolean)
//       Living (type System.Boolean)
Imports System.Reflection

Module Example
   Public Sub Main()
      Dim assem As Assembly = GetType(Example).Assembly
      Dim t As Type = assem.GetType("Transportation.MeansOfTransportation")
      If Not t Is Nothing Then
         Console.WriteLine("Virtual properties in type {0}:", 
                           t.FullName)
         Dim props() As PropertyInfo = t.GetProperties()
         Dim nVirtual As Integer = 0
         For ctr As Integer = 0 To props.Length - 1
            If props(ctr).GetMethod.IsVirtual Then
               Console.WriteLine("   {0} (type {1})",
                                 props(ctr).Name, 
                                 props(ctr).PropertyType.FullName)
               nVirtual += 1
            End If
         Next
         If nVirtual = 0 Then 
            Console.WriteLine("   No virtual properties")
         End If   
      End If   
   End Sub
End Module

Namespace Transportation
   Public MustInherit Class MeansOfTransportation
      Public MustOverride Property HasWheels As Boolean
      Public MustOverride Property Wheels As Integer
      Public MustOverride Property ConsumesFuel As Boolean
      Public MustOverride Property Living As Boolean
   End Class
End Namespace
' The example displays the following output:
'    Virtual properties in type Transportation.MeansOfTransportation:
'       HasWheels (type System.Boolean)
'       Wheels (type System.Int32)
'       ConsumesFuel (type System.Boolean)
'       Living (type System.Boolean)

Commenti

Questo metodo esegue la ricerca solo nell'istanza dell'assembly corrente. Il name parametro include lo spazio dei nomi ma non l'assembly. Per cercare un tipo in altri assembly, usare l'overload del Type.GetType(String) metodo, che può facoltativamente includere un nome visualizzato dell'assembly come parte del nome del tipo.

In .NET Core/.NET 5+, se sono presenti parametri di tipo generico qualificati per assembly nella stringa del nome del tipo, tali riferimenti all'assembly verranno caricati dal AssemblyLoadContext del metodo che ha chiamato Assembly.GetType o dal contesto AssemblyLoadContext.CurrentContextualReflectionContext se impostato.

Annotazioni

Se il tipo è stato inoltrato a un altro assembly, viene comunque restituito da questo metodo. Per informazioni sull'inoltro dei tipi, vedere Inoltro dei tipi in Common Language Runtime.

Si applica a