Module.GetType Metodo

Definizione

Restituisce il tipo specificato.

Overload

GetType(String)

Restituisce il tipo specificato, eseguendo una ricerca con distinzione tra maiuscole e minuscole.

GetType(String, Boolean)

Restituisce il tipo specificato eseguendo la ricerca nel modulo con la distinzione tra maiuscole e minuscole specificata.

GetType(String, Boolean, Boolean)

Restituisce il tipo specificato e indica se eseguire una ricerca nel modulo con la distinzione tra maiuscole e minuscole e se deve essere generata un'eccezione se il tipo non viene trovato.

GetType(String)

Restituisce il tipo specificato, eseguendo una ricerca con distinzione tra maiuscole e minuscole.

public:
 virtual Type ^ GetType(System::String ^ className);
public virtual Type? GetType (string className);
public virtual Type GetType (string className);
[System.Runtime.InteropServices.ComVisible(true)]
public virtual Type GetType (string className);
override this.GetType : string -> Type
[<System.Runtime.InteropServices.ComVisible(true)>]
override this.GetType : string -> Type
Public Overridable Function GetType (className As String) As Type

Parametri

className
String

Nome del tipo da trovare. Il nome deve essere completo con lo spazio dei nomi.

Restituisce

Type

Oggetto Type che rappresenta il tipo specificato, se il tipo è presente nel modulo; in caso contrario, null.

Attributi

Eccezioni

className è null.

Gli inizializzatori di classi vengono richiamati e viene generata un'eccezione.

className è una stringa di lunghezza zero.

className richiede un assembly dipendente che non è stato trovato.

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

className richiede un assembly dipendente, ma il file non è un assembly valido.

-oppure- className richiede un assembly dipendente che è stato compilato per una versione del runtime successiva a quella attualmente caricata.

Esempio

Nell'esempio seguente viene visualizzato il nome di un tipo nel modulo specificato.

using namespace System;
using namespace System::Reflection;

namespace ReflectionModule_Examples
{
   public ref class MyMainClass{};

}

int main()
{
   array<Module^>^moduleArray;
   moduleArray = ReflectionModule_Examples::MyMainClass::typeid->Assembly->GetModules( false );
   
   //In a simple project with only one module, the module at index
   // 0 will be the module containing these classes.
   Module^ myModule = moduleArray[ 0 ];
   Type^ myType;
   myType = myModule->GetType( "ReflectionModule_Examples.MyMainClass" );
   Console::WriteLine( "Got type: {0}", myType );
}
using System;
using System.Reflection;

namespace ReflectionModule_Examples
{
    class MyMainClass
    {
        static void Main()
        {
            Module[] moduleArray;
            
            moduleArray = typeof(MyMainClass).Assembly.GetModules(false);
            
            //In a simple project with only one module, the module at index
            // 0 will be the module containing these classes.
            Module myModule = moduleArray[0];

            Type myType;

            myType = myModule.GetType("ReflectionModule_Examples.MyMainClass");
            Console.WriteLine("Got type: {0}", myType.ToString());
        }
    }
}
Imports System.Reflection

'This code assumes that the root namespace is set to empty("").
Namespace ReflectionModule_Examples
    Class MyMainClass
        Shared Sub Main()
            Dim moduleArray() As [Module]

            moduleArray = GetType(MyMainClass).Assembly.GetModules(False)

            'In a simple project with only one module, the module at index
            ' 0 will be the module containing these classes.
            Dim myModule As [Module] = moduleArray(0)

            Dim myType As Type

            myType = myModule.GetType("ReflectionModule_Examples.MyMainClass")
            Console.WriteLine("Got type: {0}", myType.ToString())
        End Sub
    End Class
End Namespace 'ReflectionModule_Examples

Commenti

Nota

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

Un tipo può essere recuperato da un modulo specifico usando Module.GetType . La Module.GetType chiamata al modulo che contiene il manifesto non consente di cercare l'intero assembly. Per recuperare un tipo da un assembly, indipendentemente dal modulo in cui si trova, è necessario chiamare Assembly.GetType .

Si applica a

GetType(String, Boolean)

Restituisce il tipo specificato eseguendo la ricerca nel modulo con la distinzione tra maiuscole e minuscole specificata.

public:
 virtual Type ^ GetType(System::String ^ className, bool ignoreCase);
public virtual Type? GetType (string className, bool ignoreCase);
public virtual Type GetType (string className, bool ignoreCase);
[System.Runtime.InteropServices.ComVisible(true)]
public virtual Type GetType (string className, bool ignoreCase);
override this.GetType : string * bool -> Type
[<System.Runtime.InteropServices.ComVisible(true)>]
override this.GetType : string * bool -> Type
Public Overridable Function GetType (className As String, ignoreCase As Boolean) As Type

Parametri

className
String

Nome del tipo da trovare. Il nome deve essere completo con lo spazio dei nomi.

ignoreCase
Boolean

true per eseguire la ricerca senza distinzione tra maiuscole e minuscole; in caso contrario, false.

Restituisce

Type

Oggetto Type che rappresenta il tipo specificato, se il tipo è presente nel modulo; in caso contrario, null.

Attributi

Eccezioni

className è null.

Gli inizializzatori di classi vengono richiamati e viene generata un'eccezione.

className è una stringa di lunghezza zero.

className richiede un assembly dipendente che non è stato trovato.

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

className richiede un assembly dipendente, ma il file non è un assembly valido.

-oppure- className richiede un assembly dipendente che è stato compilato per una versione del runtime successiva a quella attualmente caricata.

Esempio

Nell'esempio seguente viene visualizzato il nome di un tipo nel modulo specificato, specificando per il parametro in modo che la distinzione tra maiuscole e false ignoreCase minuscole non sia ignorata.

using namespace System;
using namespace System::Reflection;

namespace ReflectionModule_Examples
{
   public ref class MyMainClass{};

}

int main()
{
   array<Module^>^moduleArray;
   moduleArray = ReflectionModule_Examples::MyMainClass::typeid->Assembly->GetModules( false );
   
   //In a simple project with only one module, the module at index
   // 0 will be the module containing these classes.
   Module^ myModule = moduleArray[ 0 ];
   Type^ myType;
   myType = myModule->GetType( "ReflectionModule_Examples.MyMainClass", false );
   Console::WriteLine( "Got type: {0}", myType );
}
using System;
using System.Reflection;

namespace ReflectionModule_Examples
{
    class MyMainClass
    {
        static void Main()
        {
            Module[] moduleArray;
            
            moduleArray = typeof(MyMainClass).Assembly.GetModules(false);
            
            //In a simple project with only one module, the module at index
            // 0 will be the module containing these classes.
            Module myModule = moduleArray[0];

            Type myType;
            myType = myModule.GetType("ReflectionModule_Examples.MyMainClass", false);
            Console.WriteLine("Got type: {0}", myType.ToString());
        }
    }
}
Imports System.Reflection

'This code assumes that the root namespace is set to empty("").
Namespace ReflectionModule_Examples
    Class MyMainClass
        Shared Sub Main()
            Dim moduleArray() As [Module]

            moduleArray = GetType(MyMainClass).Assembly.GetModules(False)

            'In a simple project with only one module, the module at index
            ' 0 will be the module containing these classes.
            Dim myModule As [Module] = moduleArray(0)

            Dim myType As Type
            myType = myModule.GetType("ReflectionModule_Examples.MyMainClass", False)
            Console.WriteLine("Got type: {0}", myType.ToString())
        End Sub
    End Class
End Namespace 'ReflectionModule_Examples

Commenti

Nota

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

Un tipo può essere recuperato da un modulo specifico usando Module.GetType . La Module.GetType chiamata al modulo che contiene il manifesto non consente di cercare l'intero assembly. Per recuperare un tipo da un assembly, indipendentemente dal modulo in cui si trova, è necessario chiamare Assembly.GetType .

Si applica a

GetType(String, Boolean, Boolean)

Restituisce il tipo specificato e indica se eseguire una ricerca nel modulo con la distinzione tra maiuscole e minuscole e se deve essere generata un'eccezione se il tipo non viene trovato.

public:
 virtual Type ^ GetType(System::String ^ className, bool throwOnError, bool ignoreCase);
public virtual Type GetType (string className, bool throwOnError, bool ignoreCase);
public virtual Type? GetType (string className, bool throwOnError, bool ignoreCase);
[System.Runtime.InteropServices.ComVisible(true)]
public virtual Type GetType (string className, bool throwOnError, bool ignoreCase);
override this.GetType : string * bool * bool -> Type
[<System.Runtime.InteropServices.ComVisible(true)>]
override this.GetType : string * bool * bool -> Type
Public Overridable Function GetType (className As String, throwOnError As Boolean, ignoreCase As Boolean) As Type

Parametri

className
String

Nome del tipo da trovare. Il nome deve essere completo con lo spazio dei nomi.

throwOnError
Boolean

true per generare un'eccezione se non è possibile trovare il tipo; false per restituire null.

ignoreCase
Boolean

true per eseguire la ricerca senza distinzione tra maiuscole e minuscole; in caso contrario, false.

Restituisce

Type

Oggetto Type che rappresenta il tipo specificato, se il tipo è dichiarato in questo modulo. In caso contrario, null.

Attributi

Eccezioni

className è null.

Gli inizializzatori di classi vengono richiamati e viene generata un'eccezione.

className è una stringa di lunghezza zero.

throwOnError è true e il tipo non è stato trovato.

className richiede un assembly dipendente che non è stato trovato.

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

className richiede un assembly dipendente, ma il file non è un assembly valido.

-oppure- className richiede un assembly dipendente che è stato compilato per una versione del runtime successiva a quella attualmente caricata.

Esempio

Nell'esempio seguente viene visualizzato il nome di un tipo nel modulo specificato. I throwOnError parametri e sono specificati come ignoreCase false .

using namespace System;
using namespace System::Reflection;

namespace ReflectionModule_Examples
{
   public ref class MyMainClass{};

}

int main()
{
   array<Module^>^moduleArray;
   moduleArray = ReflectionModule_Examples::MyMainClass::typeid->Assembly->GetModules( false );
   
   //In a simple project with only one module, the module at index
   // 0 will be the module containing this class.
   Module^ myModule = moduleArray[ 0 ];
   Type^ myType;
   myType = myModule->GetType( "ReflectionModule_Examples.MyMainClass", false, false );
   Console::WriteLine( "Got type: {0}", myType );
}
using System;
using System.Reflection;

namespace ReflectionModule_Examples
{
    class MyMainClass
    {
        static void Main()
        {
            Module[] moduleArray;
            
            moduleArray = typeof(MyMainClass).Assembly.GetModules(false);
            
            //In a simple project with only one module, the module at index
            // 0 will be the module containing this class.
            Module myModule = moduleArray[0];

            Type myType;
            myType = myModule.GetType("ReflectionModule_Examples.MyMainClass", false, false);
            Console.WriteLine("Got type: {0}", myType.ToString());
        }
    }
}
Imports System.Reflection

'This code assumes that the root namespace is set to empty("").
Namespace ReflectionModule_Examples
    Class MyMainClass
        Shared Sub Main()
            Dim moduleArray() As [Module]

            moduleArray = GetType(MyMainClass).Assembly.GetModules(False)

            'In a simple project with only one module, the module at index
            ' 0 will be the module containing this class.
            Dim myModule As [Module] = moduleArray(0)

            Dim myType As Type
            myType = myModule.GetType("ReflectionModule_Examples.MyMainClass", False, False)
            Console.WriteLine("Got type: {0}", myType.ToString())
        End Sub
    End Class
End Namespace 'ReflectionModule_Examples

Commenti

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, può essere generato TypeLoadException anche se throwOnError è false .

Nota

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

Un tipo può essere recuperato da un modulo specifico usando Module.GetType . La Module.GetType chiamata al modulo che contiene il manifesto non consente di cercare l'intero assembly. Per recuperare un tipo da un assembly, indipendentemente dal modulo in cui si trova, è necessario chiamare Assembly.GetType .

Si applica a