Assembly.GetType Metodo

Definizione

Ottiene l'oggetto Type che rappresenta il tipo specificato.

Overload

GetType(String, Boolean, Boolean)

Ottiene l'oggetto Type con il nome specificato nell'istanza dell'assembly, con la possibilità di ignorare la distinzione tra maiuscole e minuscole 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(String)

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

GetType()

GetType(String, Boolean, Boolean)

Ottiene l'oggetto Type con il nome specificato nell'istanza dell'assembly, con la possibilità di ignorare la distinzione tra maiuscole e minuscole 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 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 nel nome del tipo; in caso contrario, false.

Restituisce

Oggetto che rappresenta la classe specificata.

Implementazioni

Eccezioni

name non è valido.

-oppure-

La lunghezza name supera i 1024 caratteri.

name è null.

throwOnError è true e il tipo non è stato trovato.

name richiede un assembly dipendente che non è stato trovato.

name richiede un assembly dipendente che è stato 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.

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

Commenti

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

Nota

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);
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.

Restituisce

Oggetto che rappresenta la classe specificata.

Implementazioni

Eccezioni

name non è valido.

-oppure-

La lunghezza name supera i 1024 caratteri.

name è null.

throwOnError è true e il tipo non è stato trovato.

name richiede un assembly dipendente che non è stato trovato.

name richiede un assembly dipendente che è stato 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.

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

Commenti

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

Nota

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)

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);
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.

Restituisce

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 che è stato 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.

Nota: in .NET per le app di Windows Store o la libreria di classi portabili, rilevare invece l'eccezione della classe di base, , IOException.

typeName 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 Transportation spazio dei nomi. Chiama il metodo per recuperare Type l'oggetto, chiama il Type.GetPropertiesGetType(String) 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 lo 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 solo ricerche nell'istanza dell'assembly corrente. Il name parametro include lo spazio dei nomi ma non l'assembly. Per cercare altri assembly per un tipo, usare l'overload del metodo, che può facoltativamente includere un nome visualizzato dell'assembly Type.GetType(String) come parte del nome del tipo.

Nota

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

GetType()

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

Restituisce

Implementazioni

Si applica a