Assembly.GetType Assembly.GetType Assembly.GetType Assembly.GetType Method

定義

取得代表指定類型的 Type 物件。Gets the Type object that represents the specified type.

多載

GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean)

使用忽略大小寫和找不到類型時擲回例外狀況的選項,取得具有組件執行個體中指定之名稱的 Type 物件。Gets the Type object with the specified name in the assembly instance, with the options of ignoring the case, and of throwing an exception if the type is not found.

GetType(String, Boolean) GetType(String, Boolean) GetType(String, Boolean) GetType(String, Boolean)

取得具有組件執行個體中指定之名稱的 Type 物件,並在找不到類型時選擇性地擲回例外狀況。Gets the Type object with the specified name in the assembly instance and optionally throws an exception if the type is not found.

GetType(String) GetType(String) GetType(String) GetType(String)

取得具有組件執行個體中指定名稱的 Type 物件。Gets the Type object with the specified name in the assembly instance.

GetType() GetType() GetType() GetType()

GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean) GetType(String, Boolean, Boolean)

使用忽略大小寫和找不到類型時擲回例外狀況的選項,取得具有組件執行個體中指定之名稱的 Type 物件。Gets the Type object with the specified name in the assembly instance, with the options of ignoring the case, and of throwing an exception if the type is not found.

public:
 virtual Type ^ GetType(System::String ^ name, bool throwOnError, bool ignoreCase);
public virtual 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

參數

name
String String String String

類型的完整名稱。The full name of the type.

throwOnError
Boolean Boolean Boolean Boolean

true 表示找不到該類型時擲回例外狀況,false 則表示傳回 nulltrue to throw an exception if the type is not found; false to return null.

ignoreCase
Boolean Boolean Boolean Boolean

若要忽略類型名稱的大小寫,則為 true,否則為 falsetrue to ignore the case of the type name; otherwise, false.

傳回

表示指定之類別的物件。An object that represents the specified class.

實作

例外狀況

name 無效。name is invalid.

-或--or- name 的長度超過 1024 個字元。The length of name exceeds 1024 characters.

throwOnErrortrue,而且找不到類型。throwOnError is true, and the type cannot be found.

找不到 name 所需的相依組件。name requires a dependent assembly that could not be found.

找到 name 所需的相依組件,但無法載入。name requires a dependent assembly that was found but could not be loaded.

-或--or- 目前的組件已載入僅限反映的內容中,而且 name 需要未預先載入的相依組件。The current assembly was loaded into the reflection-only context, and name requires a dependent assembly that was not preloaded.

name 需要相依組件,但這個檔案不是有效的組件。name requires a dependent assembly, but the file is not a valid assembly.

-或--or- name 所需的相依組件是針對比目前載入之版本還新的執行階段版本所編譯。name requires a dependent assembly which was compiled for a version of the runtime later than the currently loaded version.

備註

這個方法只會搜尋目前組件執行個體。This method only searches the current assembly instance. name參數包含命名空間,但不是含組件。The name parameter includes the namespace but not the assembly. 若要搜尋型別的其他組件,請使用Type.GetType(String)方法多載,可以選擇性地包含組件顯示名稱的型別名稱的一部分。To search other assemblies for a type, use the Type.GetType(String) method overload, which can optionally include an assembly display name as part of the type name.

注意

如果類型已轉送至另一個組件,它仍會傳回這個方法。If the type has been forwarded to another assembly, it is still returned by this method. 類型轉送上的資訊,請參閱Common Language Runtime 中的型別轉送For information on type forwarding, see Type Forwarding in the Common Language Runtime.

throwOnError參數只會影響找不到類型時,會發生什麼事。The throwOnError parameter only affects what happens when the type is not found. 它不會影響其他可能會擲回的例外狀況。It does not affect any other exceptions that might be thrown. 特別是,如果找到的型別,但無法載入,TypeLoadException可能會擲回即使throwOnErrorfalseIn particular, if the type is found but cannot be loaded, TypeLoadException can be thrown even if throwOnError is false.

GetType(String, Boolean) GetType(String, Boolean) GetType(String, Boolean) GetType(String, Boolean)

取得具有組件執行個體中指定之名稱的 Type 物件,並在找不到類型時選擇性地擲回例外狀況。Gets the Type object with the specified name in the assembly instance and optionally throws an exception if the type is not found.

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

參數

name
String String String String

類型的完整名稱。The full name of the type.

throwOnError
Boolean Boolean Boolean Boolean

true 表示找不到該類型時擲回例外狀況,false 則表示傳回 nulltrue to throw an exception if the type is not found; false to return null.

傳回

表示指定之類別的物件。An object that represents the specified class.

實作

例外狀況

name 無效。name is invalid.

-或--or- name 的長度超過 1024 個字元。The length of name exceeds 1024 characters.

throwOnErrortrue,而且找不到類型。throwOnError is true, and the type cannot be found.

找不到 name 所需的相依組件。name requires a dependent assembly that could not be found.

找到 name 所需的相依組件,但無法載入。name requires a dependent assembly that was found but could not be loaded.

-或--or- 目前的組件已載入僅限反映的內容中,而且 name 需要未預先載入的相依組件。The current assembly was loaded into the reflection-only context, and name requires a dependent assembly that was not preloaded.

name 需要相依組件,但這個檔案不是有效的組件。name requires a dependent assembly, but the file is not a valid assembly.

-或--or- name 所需的相依組件是針對比目前載入之版本還新的執行階段版本所編譯。name requires a dependent assembly which was compiled for a version of the runtime later than the currently loaded version.

備註

這個方法只會搜尋目前組件執行個體。This method only searches the current assembly instance. name參數包含命名空間,但不是含組件。The name parameter includes the namespace but not the assembly. 若要搜尋型別的其他組件,請使用Type.GetType(String)方法多載,可以選擇性地包含組件顯示名稱的型別名稱的一部分。To search other assemblies for a type, use the Type.GetType(String) method overload, which can optionally include an assembly display name as part of the type name.

注意

如果類型已轉送至另一個組件,它仍會傳回這個方法。If the type has been forwarded to another assembly, it is still returned by this method. 類型轉送上的資訊,請參閱Common Language Runtime 中的型別轉送For information on type forwarding, see Type Forwarding in the Common Language Runtime.

throwOnError參數只會影響找不到類型時,會發生什麼事。The throwOnError parameter only affects what happens when the type is not found. 它不會影響其他可能會擲回的例外狀況。It does not affect any other exceptions that might be thrown. 特別是,如果找到的型別,但無法載入,TypeLoadException可能會擲回即使throwOnErrorfalseIn particular, if the type is found but cannot be loaded, TypeLoadException can be thrown even if throwOnError is false.

GetType(String) GetType(String) GetType(String) GetType(String)

取得具有組件執行個體中指定名稱的 Type 物件。Gets the Type object with the specified name in the assembly instance.

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

參數

name
String String String String

類型的完整名稱。The full name of the type.

傳回

物件,表示指定的類別,如果找不到類別,則為 nullAn object that represents the specified class, or null if the class is not found.

實作

例外狀況

找不到 name 所需的相依組件。name requires a dependent assembly that could not be found.


適用於 Windows 市集應用程式的 .NET 或是可攜式類別庫中,改為攔截基底類別例外狀況IOExceptionIn the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, IOException, instead.

找到 name 所需的相依組件,但無法載入。 name requires a dependent assembly that was found but could not be loaded.

-或--or- 目前的組件已載入僅限反映的內容中,而且 name 需要未預先載入的相依組件。The current assembly was loaded into the reflection-only context, and name requires a dependent assembly that was not preloaded.

name 需要相依組件,但這個檔案不是有效的組件。name requires a dependent assembly, but the file is not a valid assembly.

-或--or- name 所需的相依組件是針對比目前載入之版本還新的執行階段版本所編譯。name requires a dependent assembly which was compiled for a version of the runtime later than the currently loaded version.

範例

下列範例會定義抽象MeansOfTransportation類別中Transportation命名空間。The following example defines an abstract MeansOfTransportation class in the Transportation namespace. 它會呼叫GetType(String)方法來擷取其Type物件時,會呼叫Type.GetProperties方法來取得各種PropertyInfo代表類型的屬性,然後在類型上顯示資訊的物件抽象屬性。It calls the GetType(String) method to retrieve its Type object, calls the Type.GetProperties method to get an array of PropertyInfo objects that represent the type's properties, and then displays information on the type's abstract properties. 請注意,呼叫GetType(String)方法會使用型別的完整格式的名稱 (亦即,其型別名稱以及其命名空間)。Note that the call to the GetType(String) method uses the type's fully qualified name (that is, its namespace along with its type name).

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 {0}:", 
                           t.FullName);
         PropertyInfo[] props = t.GetProperties();
         int nVirtual = 0;
         for (int ctr = 0; ctr < props.Length; ctr++)
            if (props[ctr].GetMethod.IsVirtual) {
               Console.WriteLine("   {0} (type {1})",
                                 props[ctr].Name, 
                                 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)

備註

這個方法只會搜尋目前組件執行個體。This method only searches the current assembly instance. name參數包含命名空間,但不是含組件。The name parameter includes the namespace but not the assembly. 若要搜尋型別的其他組件,請使用Type.GetType(String)方法多載,可以選擇性地包含組件顯示名稱的型別名稱的一部分。To search other assemblies for a type, use the Type.GetType(String) method overload, which can optionally include an assembly display name as part of the type name.

注意

如果類型已轉送至另一個組件,它仍會傳回這個方法。If the type has been forwarded to another assembly, it is still returned by this method. 類型轉送上的資訊,請參閱Common Language Runtime 中的型別轉送For information on type forwarding, see Type Forwarding in the Common Language Runtime.

GetType() GetType() GetType() GetType()

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

傳回

實作

適用於