Module.GetType 方法

定義

傳回指定的型別。

多載

GetType(String)

執行區分大小寫的搜尋,傳回指定的類型。

GetType(String, Boolean)

傳回指定的類型,使用指定的大小寫搜尋模組。

GetType(String, Boolean, Boolean)

傳回指定的型別,並指定是否要對模組進行區分大小寫的搜尋,以及是否要在找不到型別時擲回例外狀況。

GetType(String)

執行區分大小寫的搜尋,傳回指定的類型。

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

參數

className
String

要尋找的類型名稱。 名稱必須是命名空間的完整名稱。

傳回

Type

Type 物件,如果指定的類型位於此模組中,則代表該類型;否則為 null

屬性

例外狀況

classNamenull

叫用了類別初始設定式,並擲回例外狀況。

className 是零長度字串

找不到 className 所需的相依組件。

找到 className 所需的相依組件,但無法載入。

-或- 目前的組件已載入僅限反映的內容中,而且 className 需要未預先載入的相依組件。

className 需要相依組件,但這個檔案不是有效的組件。

-或- className 所需的相依組件是針對比目前載入之版本還新的執行階段版本所編譯。

範例

下列範例會顯示指定模組中的型別名稱。

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

備註

注意

如果類型已轉送至另一個元件,則這個方法仍會傳回該類型。 如需類型轉送的詳細資訊,請參閱 Common Language Runtime 中的類型轉送

您可以使用從特定模組抓取的型別 Module.GetTypeModule.GetType在包含資訊清單的模組上呼叫,將不會搜尋整個元件。 若要從元件取出型別,不論它在哪一個模組中,您都必須呼叫 Assembly.GetType

適用於

GetType(String, Boolean)

傳回指定的類型,使用指定的大小寫搜尋模組。

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

參數

className
String

要尋找的類型名稱。 名稱必須是命名空間的完整名稱。

ignoreCase
Boolean

true 以進行不區分大小寫的搜尋;否則為 false

傳回

Type

Type 物件,如果指定的類型位於此模組中,則代表該類型;否則為 null

屬性

例外狀況

classNamenull

叫用了類別初始設定式,並擲回例外狀況。

className 是零長度字串

找不到 className 所需的相依組件。

找到 className 所需的相依組件,但無法載入。

-或- 目前的組件已載入僅限反映的內容中,而且 className 需要未預先載入的相依組件。

className 需要相依組件,但這個檔案不是有效的組件。

-或- className 所需的相依組件是針對比目前載入之版本還新的執行階段版本所編譯。

範例

下列範例會在指定的模組中顯示類型的名稱, false 為參數指定, ignoreCase 如此就不會忽略大小寫。

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

備註

注意

如果類型已轉送至另一個元件,則這個方法仍會傳回該類型。 如需類型轉送的詳細資訊,請參閱 Common Language Runtime 中的類型轉送

您可以使用從特定模組抓取的型別 Module.GetTypeModule.GetType在包含資訊清單的模組上呼叫,將不會搜尋整個元件。 若要從元件取出型別,不論它在哪一個模組中,您都必須呼叫 Assembly.GetType

適用於

GetType(String, Boolean, Boolean)

傳回指定的型別,並指定是否要對模組進行區分大小寫的搜尋,以及是否要在找不到型別時擲回例外狀況。

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

參數

className
String

要尋找的類型名稱。 名稱必須是命名空間的完整名稱。

throwOnError
Boolean

true 表示找不到該類型時擲回例外狀況,而 false 則表示傳回 null

ignoreCase
Boolean

true 以進行不區分大小寫的搜尋;否則為 false

傳回

Type

如果在這個模組中宣告型別,則為表示指定之型別的 Type 物件,否則為 null

屬性

例外狀況

classNamenull

叫用了類別初始設定式,並擲回例外狀況。

className 是零長度字串

throwOnErrortrue,而且找不到類型。

找不到 className 所需的相依組件。

找到 className 所需的相依組件,但無法載入。

-或- 目前的組件已載入僅限反映的內容中,而且 className 需要未預先載入的相依組件。

className 需要相依組件,但這個檔案不是有效的組件。

-或- className 所需的相依組件是針對比目前載入之版本還新的執行階段版本所編譯。

範例

下列範例會顯示指定模組中的型別名稱。 throwOnErrorignoreCase 參數會指定為 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

備註

throwOnError參數只會影響找不到型別時所發生的情況。 它不會影響任何其他可能擲回的例外狀況。 尤其是,如果找到類型但無法載入,即使為,也可以擲回 TypeLoadException throwOnError false

注意

如果類型已轉送至另一個元件,則這個方法仍會傳回該類型。 如需類型轉送的詳細資訊,請參閱 Common Language Runtime 中的類型轉送

您可以使用從特定模組抓取的型別 Module.GetTypeModule.GetType在包含資訊清單的模組上呼叫,將不會搜尋整個元件。 若要從元件取出型別,不論它在哪一個模組中,您都必須呼叫 Assembly.GetType

適用於