Assembly.GetType 方法

定義

取得代表指定類型的 Type 物件。

多載

GetType(String, Boolean, Boolean)

使用忽略大小寫和找不到類型時擲回例外狀況的選項,取得具有組件執行個體中指定之名稱的 Type 物件。

GetType(String, Boolean)

取得具有組件執行個體中指定之名稱的 Type 物件,並在找不到類型時選擇性地擲回例外狀況。

GetType(String)

取得具有組件執行個體中指定名稱的 Type 物件。

GetType()

GetType(String, Boolean, Boolean)

來源:
Assembly.cs
來源:
Assembly.cs
來源:
Assembly.cs

使用忽略大小寫和找不到類型時擲回例外狀況的選項,取得具有組件執行個體中指定之名稱的 Type 物件。

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

參數

name
String

類型的完整名稱。

throwOnError
Boolean

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

ignoreCase
Boolean

若要忽略類型名稱的大小寫,則為 true,否則為 false

傳回

表示指定之類別的物件。

實作

例外狀況

name 無效。

-或-

name 的長度超過 1024 個字元。

namenull

throwOnErrortrue,而且找不到類型。

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

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

-或-

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

typeName 需要相依元件,但檔案不是目前載入運行時間的有效元件。

備註

這個方法只會搜尋目前的元件實例。 參數 name 包含 命名空間,但不包含元件。 若要搜尋類型的其他元件,請使用 Type.GetType(String) 方法多載,其可以選擇性地包含元件顯示名稱做為類型名稱的一部分。

注意

如果類型已轉送到另一個元件,此方法仍會傳回它。 如需類型轉送的資訊,請參閱 Common Language Runtime 中的類型轉送

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

適用於

GetType(String, Boolean)

來源:
Assembly.cs
來源:
Assembly.cs
來源:
Assembly.cs

取得具有組件執行個體中指定之名稱的 Type 物件,並在找不到類型時選擇性地擲回例外狀況。

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

參數

name
String

類型的完整名稱。

throwOnError
Boolean

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

傳回

表示指定之類別的物件。

實作

例外狀況

name 無效。

-或-

name 的長度超過 1024 個字元。

namenull

throwOnErrortrue,而且找不到類型。

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

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

-或-

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

typeName 需要相依元件,但檔案不是目前載入運行時間的有效元件。

備註

這個方法只會搜尋目前的元件實例。 參數 name 包含 命名空間,但不包含元件。 若要搜尋類型的其他元件,請使用 Type.GetType(String) 方法多載,其可以選擇性地包含元件顯示名稱做為類型名稱的一部分。

注意

如果類型已轉送到另一個元件,此方法仍會傳回它。 如需類型轉送的資訊,請參閱 Common Language Runtime 中的類型轉送

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

適用於

GetType(String)

來源:
Assembly.cs
來源:
Assembly.cs
來源:
Assembly.cs

取得具有組件執行個體中指定名稱的 Type 物件。

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

參數

name
String

類型的完整名稱。

傳回

物件,表示指定的類別,如果找不到類別,則為 null

實作

例外狀況

name 無效。

namenull

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

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

-或-

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

注意:在 適用於 Windows 市集應用程式的 .NET可攜式類別庫中,改為攔截基類例外狀況 IOException

typeName 需要相依元件,但檔案不是目前載入運行時間的有效元件。

範例

下列範例會在 命名空間中Transportation定義抽象MeansOfTransportation類。 它會呼叫 GetType(String) 方法來擷取其 Type 物件、呼叫 Type.GetProperties 方法來取得代表類型屬性的物件陣列 PropertyInfo ,然後顯示類型抽象屬性的相關信息。 請注意,對方法的呼叫 GetType(String) 會使用類型的完整名稱 (,也就是其命名空間及其類型名稱) 。

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)

備註

這個方法只會搜尋目前的元件實例。 參數 name 包含 命名空間,但不包含元件。 若要搜尋類型的其他元件,請使用 Type.GetType(String) 方法多載,其可以選擇性地包含元件顯示名稱做為類型名稱的一部分。

注意

如果類型已轉送到另一個元件,此方法仍會傳回它。 如需類型轉送的資訊,請參閱 Common Language Runtime 中的類型轉送

適用於

GetType()

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

傳回

實作

適用於