Type.GetType Type.GetType Type.GetType Type.GetType Method

定義

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

多載

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

取得目前的 TypeGets the current Type.

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

取得具有指定名稱的 Type,執行區分大小寫的搜尋。Gets the Type with the specified name, performing a case-sensitive search.

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

取得具有指定名稱的 Type,執行區分大小寫的搜尋,並設定找不到該類型時,是否應擲回例外狀況。Gets the Type with the specified name, performing a case-sensitive search and specifying whether to throw an exception if the type is not found.

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

取得具有指定名稱的 Type,指定找不到類型時是否要擲回例外狀況,以及是否要執行區分大小寫的搜尋。Gets the Type with the specified name, specifying whether to throw an exception if the type is not found and whether to perform a case-sensitive search.

GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>)

取得具有指定名稱的類型,並選擇性提供自訂方法來解析組件和類型。Gets the type with the specified name, optionally providing custom methods to resolve the assembly and the type.

GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean)

取得具有指定名稱的類型,指定找不到類型時是否擲回例外狀況,同時選擇性提供自訂方法來解析組件和類型。Gets the type with the specified name, specifying whether to throw an exception if the type is not found, and optionally providing custom methods to resolve the assembly and the type.

GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean)

取得具有指定名稱的類型,指定是否執行區分大小寫的搜尋以及找不到類型時是否擲回例外狀況,同時選擇性提供自訂方法來解析組件和類型。Gets the type with the specified name, specifying whether to perform a case-sensitive search and whether to throw an exception if the type is not found, and optionally providing custom methods to resolve the assembly and the type.

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

取得目前的 TypeGets the current Type.

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

傳回

目前的 TypeThe current Type.

實作

例外狀況

叫用類別初始設定式並擲回例外狀況。A class initializer is invoked and throws an exception.

另請參閱

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

取得具有指定名稱的 Type,執行區分大小寫的搜尋。Gets the Type with the specified name, performing a case-sensitive search.

public:
 static Type ^ GetType(System::String ^ typeName);
public static Type GetType (string typeName);
static member GetType : string -> Type
Public Shared Function GetType (typeName As String) As Type

參數

typeName
String String String String

要取得之類型的組件限定名稱。The assembly-qualified name of the type to get. 請參閱 AssemblyQualifiedNameSee AssemblyQualifiedName. 如果此類型位在目前執行的組件或 Mscorlib.dll 中,則提供其命名空間所限定的類型名稱便已足夠。If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

傳回

具有指定名稱的類型 (如有找到),否則為 nullThe type with the specified name, if found; otherwise, null.

例外狀況

叫用類別初始設定式並擲回例外狀況。A class initializer is invoked and throws an exception.

typeName 代表的泛型類型以指標類型、ByRef 類型或 Void 做為其中一個類型引數。typeName represents a generic type that has a pointer type, a ByRef type, or Void as one of its type arguments.

-或--or- typeName 代表的泛型類型具有不正確的類型引數數目。typeName represents a generic type that has an incorrect number of type arguments.

-或--or- typeName 代表泛型類型,其中一個類型引數不符合對應類型參數的條件約束。typeName represents a generic type, and one of its type arguments does not satisfy the constraints for the corresponding type parameter.

typeName 代表 TypedReference 的陣列。typeName represents an array of TypedReference.

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

找到組件或其相依性的其中一個,但無法載入。The assembly or one of its dependencies was found, but could not be loaded.

組件或組件的其中一個相依性無效。The assembly or one of its dependencies is not valid.

-或--or- 目前載入的通用語言執行平台是 2.0 版或更新的版本,但用來編譯組件的卻是更新的版本。Version 2.0 or later of the common language runtime is currently loaded, and the assembly was compiled with a later version.

範例

下列範例會擷取的型別System.Int32並使用該型別物件來顯示FullName屬性System.Int32The following example retrieves the type of System.Int32 and uses that type object to display the FullName property of System.Int32.

using namespace System;

int main()
{
   try {
      // Get the type of a specified class.
      Type^ myType1 = Type::GetType( "System.Int32" );
      Console::WriteLine( "The full name is {0}.\n", myType1->FullName );
   }
   catch ( TypeLoadException^ e ) {
       Console::WriteLine("{0}: Unable to load type System.Int32",
                          e->GetType()->Name);
   }

   try {
      // Since NoneSuch does not exist in this assembly, GetType throws a TypeLoadException.
      Type^ myType2 = Type::GetType( "NoneSuch", true );
      Console::WriteLine( "The full name is {0}.", myType2->FullName );
   }
   catch ( TypeLoadException^ e ) {
       Console::WriteLine("{0}: Unable to load type NoneSuch",
                          e->GetType()->Name);
   }

}
// The example displays the following output:
//       The full name is System.Int32.
//
//       TypeLoadException: Unable to load type NoneSuch
using System;

class Example
{
     public static void Main()
     {
         try {
             // Get the type of a specified class.
             Type myType1 = Type.GetType("System.Int32");
             Console.WriteLine("The full name is {0}.\n", myType1.FullName);
         }
         catch (TypeLoadException e)
         {
            Console.WriteLine("{0}: Unable to load type System.Int32", e.GetType().Name);
         }

         try {
             // Since NoneSuch does not exist in this assembly, GetType throws a TypeLoadException.
             Type myType2 = Type.GetType("NoneSuch", true);
             Console.WriteLine("The full name is {0}.", myType2.FullName);
         }
         catch(TypeLoadException e) {
            Console.WriteLine("{0}: Unable to load type NoneSuch", e.GetType().Name);
         }
     }
}
// The example displays the following output:
//       The full name is System.Int32.
//
//       TypeLoadException: Unable to load type NoneSuch
Class Example
     Public Shared Sub Main()
         Try
             ' Get the type of the specified class.
             Dim myType1 As Type = Type.GetType("System.Int32")
             Console.WriteLine("The full name is {0}.", myType1.FullName)
         Catch e As TypeLoadException
             Console.WriteLine("{0}: Unable to load type System.Int32",
                               e.GetType().Name)
         End Try

         Console.WriteLine()

         Try
             ' Since NoneSuch does not exist in this assembly, GetType throws a TypeLoadException.
             Dim myType2 As Type = Type.GetType("NoneSuch", True)
             Console.WriteLine("The full name is {0}.", myType2.FullName)
         Catch e As TypeLoadException
             Console.WriteLine("{0}: Unable to load type NoneSuch", e.GetType().Name)
         End Try
     End Sub
End Class
' The example displays the following output:
'       The full name is System.Int32.
'
'       TypeLoadException: Unable to load type NoneSuch

備註

您可以使用GetType方法,以取得Type另一個組件,如果您知道其組件限定的名稱,可以取得從這個型別的物件AssemblyQualifiedNameYou can use the GetType method to obtain a Type object for a type in another assembly if you know its assembly-qualified name, which can be obtained from AssemblyQualifiedName. GetType 會導致在指定的組件載入typeNameGetType causes loading of the assembly specified in typeName. 您也可以載入組件Assembly.Load方法,然後使用Assembly.GetType或是Assembly.GetTypes方法來取得Type物件。You can also load an assembly using the Assembly.Load method, and then use the Assembly.GetType or Assembly.GetTypes method to get Type objects. 如果類型是在編譯時期已知到您的程式組件中,會使用更有效率typeof在C#或GetType在 Visual Basic 中的運算子。If a type is in an assembly known to your program at compile time, it is more efficient to use typeof in C# or the GetType operator in Visual Basic.

注意

如果typeName找不到,呼叫GetType(String)方法會傳回nullIf typeName cannot be found, the call to the GetType(String) method returns null. 它不會擲回例外狀況。It does not throw an exception. 若要控制是否發生例外狀況,請呼叫的多載GetType方法具有throwOnError參數。To control whether an exception is thrown, call an overload of the GetType method that has a throwOnError parameter.

GetType 僅適用於從磁碟載入的組件。GetType only works on assemblies loaded from disk. 如果您呼叫GetType查閱使用定義動態組件中定義的型別System.Reflection.Emit服務,您可能會收到不一致的行為。If you call GetType to look up a type defined in a dynamic assembly defined using the System.Reflection.Emit services, you might get inconsistent behavior. 行為取決於動態組件是持續性的也就是,使用建立RunAndSave或是Save存取模式的System.Reflection.Emit.AssemblyBuilderAccess列舉型別。The behavior depends on whether the dynamic assembly is persistent, that is, created using the RunAndSave or Save access modes of the System.Reflection.Emit.AssemblyBuilderAccess enumeration. 如果動態組件是持續性,並已寫入磁碟之前GetType是呼叫,載入器在磁碟上找到儲存的組件、 載入該組件中,而且該組件中擷取的型別。If the dynamic assembly is persistent and has been written to disk before GetType is called, the loader finds the saved assembly on disk, loads that assembly, and retrieves the type from that assembly. 如果組件尚未儲存至磁碟GetType呼叫時,此方法會傳回nullIf the assembly has not been saved to disk when GetType is called, the method returns null. GetType 不了解暫時性動態組件;因此,呼叫GetType若要擷取型別中的暫時性動態組件傳回nullGetType does not understand transient dynamic assemblies; therefore, calling GetType to retrieve a type in a transient dynamic assembly returns null.

若要使用GetType上的動態模組,訂閱AppDomain.AssemblyResolve事件,並呼叫GetType在儲存之前。To use GetType on a dynamic module, subscribe to the AppDomain.AssemblyResolve event and call GetType before saving. 否則,您會得到兩個組件的複本在記憶體中。Otherwise, you will get two copies of the assembly in memory.

下表顯示基底類別成員會傳回由Get反映型別上時的方法。The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

成員類型Member Type StaticStatic 非靜態Non-Static
建構函式Constructor No No
欄位Field No 可以。Yes. 欄位永遠是依名稱和簽章隱藏。A field is always hide-by-name-and-signature.
Event - 事件Event 不適用Not applicable 一般類型系統規則是繼承實作屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會視為由名稱和簽章隱藏屬性。Reflection treats properties as hide-by-name-and-signature. 請參閱附註 2。See note 2 below.
方法Method No 可以。Yes. (虛擬和非虛擬) 的方法可以隱藏依名稱或依名稱和簽章隱藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
巢狀的類型Nested Type No No
屬性Property 不適用Not applicable 一般類型系統規則是繼承實作屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會視為由名稱和簽章隱藏屬性。Reflection treats properties as hide-by-name-and-signature. 請參閱附註 2。See note 2 below.
  1. 依名稱和簽章隱藏會考慮所有組件的簽章,包括自訂修飾詞,傳回類型、 參數類型、 sentinels 和 unmanaged 呼叫慣例。Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. 這是二進位比較。This is a binary comparison.

  2. 反映的屬性和事件會依名稱和簽章隱藏。For reflection, properties and events are hide-by-name-and-signature. 如果您同時有 get 和 set 存取子的屬性在基底類別中,但衍生的類別有只有 get 存取子,在衍生的類別的屬性會隱藏基底類別屬性中,並不能存取的基底類別的 setter。If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.

  3. 自訂屬性不屬於一般類型系統。Custom attributes are not part of the common type system.

陣列或 COM 型別不會搜尋除非它們已經被載入資料表可用的類別。Arrays or COM types are not searched for unless they have already been loaded into the table of available classes.

typeName 可以在型別名稱來限定其命名空間或組件限定名稱,其中包含組件名稱規格。typeName can be the type name qualified by its namespace or an assembly-qualified name that includes an assembly name specification. 請參閱 AssemblyQualifiedNameSee AssemblyQualifiedName.

如果typeName包含命名空間,但不是組件名稱,這個方法會依此順序搜尋只呼叫物件的組件和 Mscorlib.dll。If typeName includes the namespace but not the assembly name, this method searches only the calling object's assembly and Mscorlib.dll, in that order. 如果部分或完整的組件名稱完整限定類型名稱,這個方法會搜尋指定的組件中。If typeName is fully qualified with the partial or complete assembly name, this method searches in the specified assembly. 如果組件具有強式名稱,完整的組件的名稱是必要。If the assembly has a strong name, a complete assembly name is required.

AssemblyQualifiedName屬性會傳回完整的型別名稱,包括巢狀型別、 組件名稱和泛型型別引數。The AssemblyQualifiedName property returns a fully qualified type name including nested types, the assembly name, and generic type arguments. 支援通用語言執行平台的所有編譯器將都發出的簡單名稱的巢狀類別,並反映建構查詢時,根據下列慣例 mangled 的名稱。All compilers that support the common language runtime will emit the simple name of a nested class, and reflection constructs a mangled name when queried, in accordance with the following conventions.

注意

在.NET Framework 2.0 版中,處理器架構會新增至組件識別,而且可以指定為組件名稱字串的一部分。In the .NET Framework version 2.0, processor architecture is added to assembly identity, and can be specified as part of assembly name strings. 例如,"ProcessorArchitecture = msil"。For example, "ProcessorArchitecture=msil". 不過,它不包含在所傳回的字串AssemblyQualifiedName屬性,因為相容性。However, it is not included in the string returned by the AssemblyQualifiedName property, for compatibility reasons. 您也可以藉由建立載入類型AssemblyName物件,並將其傳遞至適當的多載Load方法。You can also load types by creating an AssemblyName object and passing it to an appropriate overload of the Load method. 您可以接著使用Assembly.GetType方法來載入組件類型。You can then use the Assembly.GetType method to load types from the assembly. 請參閱 AssemblyName.ProcessorArchitectureSee also AssemblyName.ProcessorArchitecture.

分隔符號Delimiter 意義Meaning
反斜線 (\)Backslash (\) 逸出字元。Escape character.
倒單引號 (')Backtick (`) 前面有一或多個位數表示的型別參數,位於結尾的泛型型別名稱的數目。Precedes one or more digits representing the number of type parameters, located at the end of the name of a generic type.
括號 ([])Brackets ([]) 將建構的泛型類型; 泛型型別引數清單在類型引數清單中,將組件限定型別。Enclose a generic type argument list, for a constructed generic type; within a type argument list, enclose an assembly-qualified type.
逗號 (,)Comma (,) 後面接著組件名稱。Precedes the Assembly name.
句號 (.)Period (.) 表示命名空間識別項。Denotes namespace identifiers.
加號 (+)Plus sign (+) 在之前的巢狀的類別。Precedes a nested class.

例如,類別的完整格式的名稱可能如下所示:For example, the fully qualified name for a class might look like this:

TopNamespace.SubNameSpace.ContainingClass+NestedClass,MyAssembly  

如果命名空間已 TopNamespace.Sub+Namespace,則字串會有之前加號 (+),以逸出字元 (\) 以防止它被解譯為巢狀分隔符號。If the namespace were TopNamespace.Sub+Namespace, then the string would have to precede the plus sign (+) with an escape character (\) to prevent it from being interpreted as a nesting separator. 反映會發出此字串,如下所示:Reflection emits this string as follows:

TopNamespace.Sub\+Namespace.ContainingClass+NestedClass,MyAssembly  

"+ +"會變成"\+\+",和 「\」 變成 「\\"。A "++" becomes "\+\+", and a "\" becomes "\\".

這個限定的名稱可以保存和更新版本用來載入TypeThis qualified name can be persisted and later used to load the Type. 搜尋並載入Type,使用GetType使用型別名稱只有或使用組件限定的類型名稱。To search for and load a Type, use GetType either with the type name only or with the assembly qualified type name. GetType 與型別名稱只會尋找Type呼叫者的組件中,然後在系統組件中。GetType with the type name only will look for the Type in the caller's assembly and then in the System assembly. GetType 與組件限定的類型名稱會尋找Type任何組件中。GetType with the assembly qualified type name will look for the Type in any assembly.

類型名稱可能包含行尾字元來表示的類型,例如類型是否為參考型別、 指標類型或陣列類型的其他資訊。Type names may include trailing characters that denote additional information about the type, such as whether the type is a reference type, a pointer type or an array type. 若要擷取的型別名稱,而不需要這些後置字元,請使用t.GetElementType().ToString(),其中t是型別。To retrieve the type name without these trailing characters, use t.GetElementType().ToString(), where t is the type.

空格是相關的組件名稱以外的所有型別名稱元件。Spaces are relevant in all type name components except the assembly name. 在組件名稱 ',' 分隔符號之前的空格是相關的但是 ',' 分隔符號之後的空格會被忽略。In the assembly name, spaces before the ',' separator are relevant, but spaces after the ',' separator are ignored.

泛型型別名稱的結尾倒引號 (`) 後面接著數字,代表泛型類型引數數目。The name of a generic type ends with a backtick (`) followed by digits representing the number of generic type arguments. 此名稱的損害的目的是讓編譯器支援泛型型別具有相同名稱但具有不同數目的型別參數,在相同的範圍內發生。The purpose of this name mangling is to allow compilers to support generic types with the same name but with different numbers of type parameters, occurring in the same scope. 例如,反映會傳回損害的名稱Tuple1andTuple2的泛型方法Tuple(Of T)Tuple(Of T0, T1)在 Visual Basic 中,或Tuple<T>和 Tuple\<T0, T1> Visual C# 中。For example, reflection returns the mangled names Tuple1andTuple2 from the generic methods Tuple(Of T) and Tuple(Of T0, T1) in Visual Basic, or Tuple<T> and Tuple\<T0, T1> in Visual C#.

為泛型類型,類型引數清單以括號、,並以逗號分隔的類型引數。For generic types, the type argument list is enclosed in brackets, and the type arguments are separated by commas. 例如,泛型Dictionary<TKey,TValue>有兩個類型參數。For example, a generic Dictionary<TKey,TValue> has two type parameters. ADictionary<TKey,TValue>MyType類型的索引鍵聯String可能如下表示:A Dictionary<TKey,TValue> of MyType with keys of type String might be represented as follows:

System.Collections.Generic.Dictionary`2[System.String,MyType]  

若要指定型別引數清單中的組件限定型別,請將括號括住的組件限定型別。To specify an assembly-qualified type within a type argument list, enclose the assembly-qualified type within brackets. 否則,逗號分隔的組件限定名稱的組件會被視為分隔其他的型別引數。Otherwise, the commas that separate the parts of the assembly-qualified name are interpreted as delimiting additional type arguments. 例如,Dictionary<TKey,TValue>MyTypefromMyAssembly.dll,類型的索引鍵聯String,可能會以下面方式指定:For example, a Dictionary<TKey,TValue> of MyType fromMyAssembly.dll, with keys of type String, might be specified as follows:

Type.GetType("System.Collections.Generic.Dictionary`2[System.String,[MyType,MyAssembly]]")  

注意

組件限定型別可以括在括弧,它必須出現在類型參數清單中。An assembly-qualified type can be enclosed in brackets only when it appears within a type parameter list. 搜尋組件限定的不合格的型別的型別參數清單中的規則並符合規定及不合格的非泛型類型的規則相同。The rules for searching assemblies for qualified and unqualified types in type parameter lists are the same as the rules for qualified and unqualified nongeneric types.

可為 null 的型別是泛型類型的特殊案例。Nullable types are a special case of generic types. 例如,可為 null Int32 "System.Nullable'1[System.Int32]"的字串表示。For example, a nullable Int32 is represented by the string "System.Nullable`1[System.Int32]".

注意

在C#, C++,以及 Visual Basic,您也可以取得可為 null 類型使用型別運算子。In C#, C++, and Visual Basic you can also get nullable types using type operators. 比方說,可為 nullBoolean型別由typeof(Nullable<bool>)在C#、 藉由Nullable<Boolean>::typeid中C++,並依據GetType(Nullable(Of Boolean))在 Visual Basic 中。For example, the nullable Boolean type is returned by typeof(Nullable<bool>) in C#, by Nullable<Boolean>::typeid in C++, and by GetType(Nullable(Of Boolean)) in Visual Basic.

下表顯示您所使用的語法GetType為各種型別。The following table shows the syntax you use with GetType for various types.

若要取得To Get 使用Use
可為 null Int32A nullable Int32 Type.GetType("System.Nullable`1[System.Int32]")
Unmanaged 的指標 MyTypeAn unmanaged pointer to MyType Type.GetType("MyType*")
指標的 unmanaged 的指標 MyTypeAn unmanaged pointer to a pointer to MyType Type.GetType("MyType**")
Managed 的指標或參考 MyTypeA managed pointer or reference to MyType Type.GetType("MyType&").Type.GetType("MyType&"). 請注意,參考與指標不同,僅限一個層級。Note that unlike pointers, references are limited to one level.
父類別和巢狀的類別A parent class and a nested class Type.GetType("MyParentClass+MyNestedClass")
一維陣列,其下限為 0A one-dimensional array with a lower bound of 0 Type.GetType("MyType[]")
下限不明的一維陣列A one-dimensional array with an unknown lower bound Type.GetType("MyType[*]")
N 維陣列An n-dimensional array 逗號 (,) 的 n-1 次數總計的方括號內。A comma (,) inside the brackets a total of n-1 times. 例如,System.Object[,,]代表三維Object陣列。For example, System.Object[,,] represents a three-dimensional Object array.
陣列的一維陣列An array of one-dimensional arrays Type.GetType("MyType[][]")
矩形的二維陣列下限不明A rectangular two-dimensional array with unknown lower bounds Type.GetType("MyType[,]")
具有一個型別引數的泛型型別A generic type with one type argument Type.GetType("MyGenericType`1[MyType]")
兩個類型引數的泛型型別A generic type with two type arguments Type.GetType("MyGenericType`2[MyType,AnotherType]")
具有兩個組件限定型別引數的泛型型別A generic type with two assembly-qualified type arguments Type.GetType("MyGenericType`2[[MyType,MyAssembly],[AnotherType,AnotherAssembly]]")
使用組件限定型別引數,組件限定的泛型類型An assembly-qualified generic type with an assembly-qualified type argument Type.GetType("MyGenericType`1[[MyType,MyAssembly]],MyGenericTypeAssembly")
其類型引數是具有兩個類型引數的泛型型別是泛型型別A generic type whose type argument is a generic type with two type arguments Type.GetType("MyGenericType`1[AnotherGenericType`2[MyType,AnotherType]]")
另請參閱

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

取得具有指定名稱的 Type,執行區分大小寫的搜尋,並設定找不到該類型時,是否應擲回例外狀況。Gets the Type with the specified name, performing a case-sensitive search and specifying whether to throw an exception if the type is not found.

public:
 static Type ^ GetType(System::String ^ typeName, bool throwOnError);
public static Type GetType (string typeName, bool throwOnError);
static member GetType : string * bool -> Type
Public Shared Function GetType (typeName As String, throwOnError As Boolean) As Type

參數

typeName
String String String String

要取得之類型的組件限定名稱。The assembly-qualified name of the type to get. 請參閱 AssemblyQualifiedNameSee AssemblyQualifiedName. 如果此類型位在目前執行的組件或 Mscorlib.dll 中,則提供其命名空間所限定的類型名稱便已足夠。If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

throwOnError
Boolean Boolean Boolean Boolean

true 表示找不到該類型時擲回例外狀況,而 false 則表示傳回 nulltrue to throw an exception if the type cannot be found; false to return null. 指定 false 也會隱藏其他某些例外狀況條件,但並不是全部。Specifying false also suppresses some other exception conditions, but not all of them. 請參閱<例外狀況>一節。See the Exceptions section.

傳回

具有指定名稱的類型。The type with the specified name. 如果找不到該類型,throwOnError 參數會指定是要傳回 null 或是擲回例外狀況。If the type is not found, the throwOnError parameter specifies whether null is returned or an exception is thrown. 在某些情況下,不論 throwOnError 的值為何,都會擲回例外狀況。In some cases, an exception is thrown regardless of the value of throwOnError. 請參閱<例外狀況>一節。See the Exceptions section.

例外狀況

叫用類別初始設定式並擲回例外狀況。A class initializer is invoked and throws an exception.

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

-或--or- throwOnErrortrue,而且 typeName 包含無效字元,例如內嵌的定位字元。throwOnError is true and typeName contains invalid characters, such as an embedded tab. -或--or- throwOnErrortrue,而且 typeName 是空字串。throwOnError is true and typeName is an empty string.

-或--or- throwOnErrortrue,而且 typeName 代表大小無效的陣列類型。throwOnError is true and typeName represents an array type with an invalid size.

-或--or- typeName 代表 TypedReference 的陣列。typeName represents an array of TypedReference.

throwOnErrortrue,而且 typeName 包含無效的語法。throwOnError is true and typeName contains invalid syntax. 例如,"MyType[,*,]"。For example, "MyType[,*,]".

-或--or- typeName 代表的泛型類型以指標類型、ByRef 類型或 Void 做為其中一個類型引數。typeName represents a generic type that has a pointer type, a ByRef type, or Void as one of its type arguments.

-或--or- typeName 代表的泛型類型具有不正確的類型引數數目。typeName represents a generic type that has an incorrect number of type arguments.

-或--or- typeName 代表泛型類型,其中一個類型引數不符合對應類型參數的條件約束。typeName represents a generic type, and one of its type arguments does not satisfy the constraints for the corresponding type parameter.

throwOnErrortrue,而且找不到組件或組件的其中一個相依性。throwOnError is true and the assembly or one of its dependencies was not found.

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

找到組件或其相依性的其中一個,但無法載入。The assembly or one of its dependencies was found, but could not be loaded.

組件或組件的其中一個相依性無效。The assembly or one of its dependencies is not valid.

-或--or- 目前載入的通用語言執行平台是 2.0 版或更新的版本,但用來編譯組件的卻是更新的版本。Version 2.0 or later of the common language runtime is currently loaded, and the assembly was compiled with a later version.

範例

下列範例會擷取的型別System.Int32並使用該型別物件來顯示FullName屬性System.Int32The following example retrieves the type of System.Int32 and uses that type object to display the FullName property of System.Int32. 如果不存在的組件參考的型別物件,此範例會擲回例外狀況。If a type object refers to an assembly that does not exist, this example throws an exception.

using namespace System;

int main()
{
   try {
      // Get the type of a specified class.
      Type^ myType1 = Type::GetType( "System.Int32" );
      Console::WriteLine( "The full name is {0}.\n", myType1->FullName );
   }
   catch ( TypeLoadException^ e ) {
       Console::WriteLine("{0}: Unable to load type System.Int32",
                          e->GetType()->Name);
   }

   try {
      // Since NoneSuch does not exist in this assembly, GetType throws a TypeLoadException.
      Type^ myType2 = Type::GetType( "NoneSuch", true );
      Console::WriteLine( "The full name is {0}.", myType2->FullName );
   }
   catch ( TypeLoadException^ e ) {
       Console::WriteLine("{0}: Unable to load type NoneSuch",
                          e->GetType()->Name);
   }

}
// The example displays the following output:
//       The full name is System.Int32.
//
//       TypeLoadException: Unable to load type NoneSuch
using System;

class Example
{
     public static void Main()
     {
         try {
             // Get the type of a specified class.
             Type myType1 = Type.GetType("System.Int32");
             Console.WriteLine("The full name is {0}.\n", myType1.FullName);
         }
         catch (TypeLoadException e)
         {
            Console.WriteLine("{0}: Unable to load type System.Int32", e.GetType().Name);
         }

         try {
             // Since NoneSuch does not exist in this assembly, GetType throws a TypeLoadException.
             Type myType2 = Type.GetType("NoneSuch", true);
             Console.WriteLine("The full name is {0}.", myType2.FullName);
         }
         catch(TypeLoadException e) {
            Console.WriteLine("{0}: Unable to load type NoneSuch", e.GetType().Name);
         }
     }
}
// The example displays the following output:
//       The full name is System.Int32.
//
//       TypeLoadException: Unable to load type NoneSuch
Class Example
     Public Shared Sub Main()
         Try
             ' Get the type of the specified class.
             Dim myType1 As Type = Type.GetType("System.Int32")
             Console.WriteLine("The full name is {0}.", myType1.FullName)
         Catch e As TypeLoadException
             Console.WriteLine("{0}: Unable to load type System.Int32",
                               e.GetType().Name)
         End Try

         Console.WriteLine()

         Try
             ' Since NoneSuch does not exist in this assembly, GetType throws a TypeLoadException.
             Dim myType2 As Type = Type.GetType("NoneSuch", True)
             Console.WriteLine("The full name is {0}.", myType2.FullName)
         Catch e As TypeLoadException
             Console.WriteLine("{0}: Unable to load type NoneSuch", e.GetType().Name)
         End Try
     End Sub
End Class
' The example displays the following output:
'       The full name is System.Int32.
'
'       TypeLoadException: Unable to load type NoneSuch

備註

您可以使用GetType方法,以取得Type另一個組件,如果您知道其組件限定的名稱,可以取得從這個型別的物件AssemblyQualifiedNameYou can use the GetType method to obtain a Type object for a type in another assembly if you know its assembly-qualified name, which can be obtained from AssemblyQualifiedName. GetType 會導致在指定的組件載入typeNameGetType causes loading of the assembly specified in typeName. 您也可以載入組件Assembly.Load方法,然後使用Assembly.GetType或是Assembly.GetTypes方法來取得Type物件。You can also load an assembly using the Assembly.Load method, and then use the Assembly.GetType or Assembly.GetTypes method to get Type objects. 如果類型是在編譯時期已知到您的程式組件中,會使用更有效率typeof在C#或GetType在 Visual Basic 中的運算子。If a type is in an assembly known to your program at compile time, it is more efficient to use typeof in C# or the GetType operator in Visual Basic.

GetType 僅適用於從磁碟載入的組件。GetType only works on assemblies loaded from disk. 如果您呼叫GetType查閱使用定義動態組件中定義的型別System.Reflection.Emit服務,您可能會收到不一致的行為。If you call GetType to look up a type defined in a dynamic assembly defined using the System.Reflection.Emit services, you might get inconsistent behavior. 行為取決於動態組件是持續性的也就是,使用建立RunAndSave或是Save存取模式的System.Reflection.Emit.AssemblyBuilderAccess列舉型別。The behavior depends on whether the dynamic assembly is persistent, that is, created using the RunAndSave or Save access modes of the System.Reflection.Emit.AssemblyBuilderAccess enumeration. 如果動態組件是持續性,並已寫入磁碟之前GetType是呼叫,載入器在磁碟上找到儲存的組件、 載入該組件中,而且該組件中擷取的型別。If the dynamic assembly is persistent and has been written to disk before GetType is called, the loader finds the saved assembly on disk, loads that assembly, and retrieves the type from that assembly. 如果組件尚未儲存至磁碟GetType呼叫時,此方法會傳回nullIf the assembly has not been saved to disk when GetType is called, the method returns null. GetType 不了解暫時性動態組件;因此,呼叫GetType若要擷取型別中的暫時性動態組件傳回nullGetType does not understand transient dynamic assemblies; therefore, calling GetType to retrieve a type in a transient dynamic assembly returns null.

若要使用GetType上的動態模組,訂閱AppDomain.AssemblyResolve事件,並呼叫GetType在儲存之前。To use GetType on a dynamic module, subscribe to the AppDomain.AssemblyResolve event and call GetType before saving. 否則,您會得到兩個組件的複本在記憶體中。Otherwise, you will get two copies of the assembly in memory.

throwOnError參數會指定時會發生什麼事的型別找不到,並會隱藏其他某些例外情況,例外狀況 」 一節中所述。The throwOnError parameter specifies what happens when the type is not found, and also suppresses certain other exception conditions, as described in the Exceptions section. 某些例外狀況的值為何throwOnErrorSome exceptions are thrown regardless of the value of throwOnError. 例如,如果找到的型別,但無法載入,TypeLoadException就會擲回即使throwOnErrorfalseFor example, if the type is found but cannot be loaded, a TypeLoadException is thrown even if throwOnError is false.

下表顯示基底類別成員會傳回由Get反映型別上時的方法。The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

成員類型Member Type StaticStatic 非靜態Non-Static
建構函式Constructor No No
欄位Field No 可以。Yes. 欄位永遠是依名稱和簽章隱藏。A field is always hide-by-name-and-signature.
Event - 事件Event 不適用Not applicable 一般類型系統規則是繼承實作屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會視為由名稱和簽章隱藏屬性。Reflection treats properties as hide-by-name-and-signature. 請參閱附註 2。See note 2 below.
方法Method No 可以。Yes. (虛擬和非虛擬) 的方法可以隱藏依名稱或依名稱和簽章隱藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
巢狀的類型Nested Type No No
屬性Property 不適用Not applicable 一般類型系統規則是繼承實作屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會視為由名稱和簽章隱藏屬性。Reflection treats properties as hide-by-name-and-signature. 請參閱附註 2。See note 2 below.
  1. 依名稱和簽章隱藏會考慮所有組件的簽章,包括自訂修飾詞,傳回類型、 參數類型、 sentinels 和 unmanaged 呼叫慣例。Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. 這是二進位比較。This is a binary comparison.

  2. 反映的屬性和事件會依名稱和簽章隱藏。For reflection, properties and events are hide-by-name-and-signature. 如果您同時有 get 和 set 存取子的屬性在基底類別中,但衍生的類別有只有 get 存取子,在衍生的類別的屬性會隱藏基底類別屬性中,並不能存取的基底類別的 setter。If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.

  3. 自訂屬性不屬於一般類型系統。Custom attributes are not part of the common type system.

陣列或 COM 型別不會搜尋除非它們已經被載入資料表可用的類別。Arrays or COM types are not searched for unless they have already been loaded into the table of available classes.

typeName 可以在型別名稱來限定其命名空間或組件限定名稱,其中包含組件名稱規格。typeName can be the type name qualified by its namespace or an assembly-qualified name that includes an assembly name specification. 請參閱 AssemblyQualifiedNameSee AssemblyQualifiedName.

如果typeName包含命名空間,但不是組件名稱,這個方法會依此順序搜尋只呼叫物件的組件和 Mscorlib.dll。If typeName includes the namespace but not the assembly name, this method searches only the calling object's assembly and Mscorlib.dll, in that order. 如果部分或完整的組件名稱完整限定類型名稱,這個方法會搜尋指定的組件中。If typeName is fully qualified with the partial or complete assembly name, this method searches in the specified assembly. 如果組件具有強式名稱,完整的組件的名稱是必要。If the assembly has a strong name, a complete assembly name is required.

AssemblyQualifiedName屬性會傳回完整的型別名稱,包括巢狀型別、 組件名稱和泛型引數。The AssemblyQualifiedName property returns a fully qualified type name including nested types, the assembly name, and generic arguments. 支援通用語言執行平台的所有編譯器將都發出的簡單名稱的巢狀類別,並反映建構查詢時,根據下列慣例 mangled 的名稱。All compilers that support the common language runtime will emit the simple name of a nested class, and reflection constructs a mangled name when queried, in accordance with the following conventions.

注意

在.NET Framework 2.0 版中,處理器架構會新增至組件識別,而且可以指定為組件名稱字串的一部分。In the .NET Framework version 2.0, processor architecture is added to assembly identity, and can be specified as part of assembly name strings. 例如,"ProcessorArchitecture = msil"。For example, "ProcessorArchitecture=msil". 不過,它不包含在所傳回的字串AssemblyQualifiedName屬性,因為相容性。However, it is not included in the string returned by the AssemblyQualifiedName property, for compatibility reasons. 您也可以藉由建立載入類型AssemblyName物件,並將其傳遞至適當的多載Load方法。You can also load types by creating an AssemblyName object and passing it to an appropriate overload of the Load method. 您可以接著使用Assembly.GetType方法來載入組件類型。You can then use the Assembly.GetType method to load types from the assembly. 請參閱 AssemblyName.ProcessorArchitectureSee also AssemblyName.ProcessorArchitecture.

分隔符號Delimiter 意義Meaning
反斜線 (\)Backslash (\) 逸出字元。Escape character.
倒單引號 (')Backtick (`) 前面有一或多個位數表示的型別參數,位於結尾的泛型型別名稱的數目。Precedes one or more digits representing the number of type parameters, located at the end of the name of a generic type.
括號 ([])Brackets ([]) 將建構的泛型類型; 泛型型別引數清單在類型引數清單中,將組件限定型別。Enclose a generic type argument list, for a constructed generic type; within a type argument list, enclose an assembly-qualified type.
逗號 (,)Comma (,) 後面接著組件名稱。Precedes the Assembly name.
句號 (.)Period (.) 表示命名空間識別項。Denotes namespace identifiers.
加號 (+)Plus sign (+) 在之前的巢狀的類別。Precedes a nested class.

例如,類別的完整格式的名稱可能如下所示:For example, the fully qualified name for a class might look like this:

TopNamespace.SubNameSpace.ContainingClass+NestedClass,MyAssembly  

如果命名空間已 TopNamespace.Sub+Namespace,則字串會有之前加號 (+),以逸出字元 (\) 以防止它被解譯為巢狀分隔符號。If the namespace were TopNamespace.Sub+Namespace, then the string would have to precede the plus sign (+) with an escape character (\) to prevent it from being interpreted as a nesting separator. 反映會發出此字串,如下所示:Reflection emits this string as follows:

TopNamespace.Sub\+Namespace.ContainingClass+NestedClass,MyAssembly  

"+ +"會變成"\+\+",和 「\」 變成 「\\"。A "++" becomes "\+\+", and a "\" becomes "\\".

這個限定的名稱可以保存和更新版本用來載入TypeThis qualified name can be persisted and later used to load the Type. 搜尋並載入Type,使用GetType使用型別名稱只有或使用組件限定的類型名稱。To search for and load a Type, use GetType either with the type name only or with the assembly qualified type name. GetType 與型別名稱只會尋找Type呼叫者的組件中,然後在系統組件中。GetType with the type name only will look for the Type in the caller's assembly and then in the System assembly. GetType 與組件限定的類型名稱會尋找Type任何組件中。GetType with the assembly qualified type name will look for the Type in any assembly.

類型名稱可能包含行尾字元來表示的類型,例如類型是否為參考型別、 指標類型或陣列類型的其他資訊。Type names may include trailing characters that denote additional information about the type, such as whether the type is a reference type, a pointer type or an array type. 若要擷取的型別名稱,而不需要這些後置字元,請使用t.GetElementType().ToString(),其中t是型別。To retrieve the type name without these trailing characters, use t.GetElementType().ToString(), where t is the type.

空格是相關的組件名稱以外的所有型別名稱元件。Spaces are relevant in all type name components except the assembly name. 在組件名稱 ',' 分隔符號之前的空格是相關的但是 ',' 分隔符號之後的空格會被忽略。In the assembly name, spaces before the ',' separator are relevant, but spaces after the ',' separator are ignored.

泛型型別名稱的結尾倒引號 (`) 後面接著數字,代表泛型類型引數數目。The name of a generic type ends with a backtick (`) followed by digits representing the number of generic type arguments. 此名稱的損害的目的是讓編譯器支援泛型型別具有相同名稱但具有不同數目的型別參數,在相同的範圍內發生。The purpose of this name mangling is to allow compilers to support generic types with the same name but with different numbers of type parameters, occurring in the same scope. 例如,反映會傳回損害的名稱Tuple1andTuple2的泛型方法Tuple(Of T)Tuple(Of T0, T1)在 Visual Basic 中,或Tuple<T>和 Tuple\<T0, T1> Visual C# 中。For example, reflection returns the mangled names Tuple1andTuple2 from the generic methods Tuple(Of T) and Tuple(Of T0, T1) in Visual Basic, or Tuple<T> and Tuple\<T0, T1> in Visual C#.

為泛型類型,類型引數清單以括號、,並以逗號分隔的類型引數。For generic types, the type argument list is enclosed in brackets, and the type arguments are separated by commas. 例如,泛型Dictionary<TKey,TValue>有兩個類型參數。For example, a generic Dictionary<TKey,TValue> has two type parameters. ADictionary<TKey,TValue>MyType類型的索引鍵聯String可能如下表示:A Dictionary<TKey,TValue> of MyType with keys of type String might be represented as follows:

System.Collections.Generic.Dictionary`2[System.String,MyType]  

若要指定型別引數清單中的組件限定型別,請將括號括住的組件限定型別。To specify an assembly-qualified type within a type argument list, enclose the assembly-qualified type within brackets. 否則,逗號分隔的組件限定名稱的組件會被視為分隔其他的型別引數。Otherwise, the commas that separate the parts of the assembly-qualified name are interpreted as delimiting additional type arguments. 例如,Dictionary<TKey,TValue>MyTypeMyAssembly.dll 中,使用索引鍵的類型從String,可能會以下面方式指定:For example, a Dictionary<TKey,TValue> of MyType from MyAssembly.dll, with keys of type String, might be specified as follows:

Type.GetType("System.Collections.Generic.Dictionary`2[System.String,[MyType,MyAssembly]]")  

注意

組件限定型別可以括在括弧,它必須出現在類型參數清單中。An assembly-qualified type can be enclosed in brackets only when it appears within a type parameter list. 搜尋組件限定的不合格的型別的型別參數清單中的規則並符合規定及不合格的非泛型類型的規則相同。The rules for searching assemblies for qualified and unqualified types in type parameter lists are the same as the rules for qualified and unqualified nongeneric types.

可為 null 的型別是泛型類型的特殊案例。Nullable types are a special case of generic types. 例如,可為 null Int32 "System.Nullable'1[System.Int32]"的字串表示。For example, a nullable Int32 is represented by the string "System.Nullable`1[System.Int32]".

注意

在C#, C++,以及 Visual Basic,您也可以取得可為 null 類型使用型別運算子。In C#, C++, and Visual Basic you can also get nullable types using type operators. 比方說,可為 nullBoolean型別由typeof(Nullable<bool>)在C#、 藉由Nullable<Boolean>::typeid中C++,並依據GetType(Nullable(Of Boolean))在 Visual Basic 中。For example, the nullable Boolean type is returned by typeof(Nullable<bool>) in C#, by Nullable<Boolean>::typeid in C++, and by GetType(Nullable(Of Boolean)) in Visual Basic.

下表顯示您所使用的語法GetType為各種型別。The following table shows the syntax you use with GetType for various types.

若要取得To Get 使用Use
可為 null Int32A nullable Int32 Type.GetType("System.Nullable`1[System.Int32]")
Unmanaged 的指標 MyTypeAn unmanaged pointer to MyType Type.GetType("MyType*")
指標的 unmanaged 的指標 MyTypeAn unmanaged pointer to a pointer to MyType Type.GetType("MyType**")
Managed 的指標或參考 MyTypeA managed pointer or reference to MyType Type.GetType("MyType&").Type.GetType("MyType&"). 請注意,參考與指標不同,僅限一個層級。Note that unlike pointers, references are limited to one level.
父類別和巢狀的類別A parent class and a nested class Type.GetType("MyParentClass+MyNestedClass")
一維陣列,其下限為 0A one-dimensional array with a lower bound of 0 Type.GetType("MyArray[]")
下限不明的一維陣列A one-dimensional array with an unknown lower bound Type.GetType("MyArray[*]")
N 維陣列An n-dimensional array 逗號 (,) 的 n-1 次數總計的方括號內。A comma (,) inside the brackets a total of n-1 times. 例如,System.Object[,,]代表三維Object陣列。For example, System.Object[,,] represents a three-dimensional Object array.
二維陣列的陣列A two-dimensional array's array Type.GetType("MyArray[][]")
矩形的二維陣列下限不明A rectangular two-dimensional array with unknown lower bounds Type.GetType("MyArray[,]")
具有一個型別引數的泛型型別A generic type with one type argument Type.GetType("MyGenericType`1[MyType]")
兩個類型引數的泛型型別A generic type with two type arguments Type.GetType("MyGenericType`2[MyType,AnotherType]")
具有兩個組件限定型別引數的泛型型別A generic type with two assembly-qualified type arguments Type.GetType("MyGenericType`2[[MyType,MyAssembly],[AnotherType,AnotherAssembly]]")
使用組件限定型別引數,組件限定的泛型類型An assembly-qualified generic type with an assembly-qualified type argument Type.GetType("MyGenericType`1[[MyType,MyAssembly]],MyGenericTypeAssembly")
其類型引數是具有兩個類型引數的泛型型別是泛型型別A generic type whose type argument is a generic type with two type arguments Type.GetType("MyGenericType`1[AnotherGenericType`2[MyType,AnotherType]]")
另請參閱

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

取得具有指定名稱的 Type,指定找不到類型時是否要擲回例外狀況,以及是否要執行區分大小寫的搜尋。Gets the Type with the specified name, specifying whether to throw an exception if the type is not found and whether to perform a case-sensitive search.

public:
 static Type ^ GetType(System::String ^ typeName, bool throwOnError, bool ignoreCase);
public static Type GetType (string typeName, bool throwOnError, bool ignoreCase);
static member GetType : string * bool * bool -> Type
Public Shared Function GetType (typeName As String, throwOnError As Boolean, ignoreCase As Boolean) As Type

參數

typeName
String String String String

要取得之類型的組件限定名稱。The assembly-qualified name of the type to get. 請參閱 AssemblyQualifiedNameSee AssemblyQualifiedName. 如果此類型位在目前執行的組件或 Mscorlib.dll 中,則提供其命名空間所限定的類型名稱便已足夠。If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

throwOnError
Boolean Boolean Boolean Boolean

true 表示找不到該類型時擲回例外狀況,而 false 則表示傳回 nulltrue to throw an exception if the type cannot be found; false to return null. 指定 false 也會隱藏其他某些例外狀況條件,但並不是全部。Specifying false also suppresses some other exception conditions, but not all of them. 請參閱<例外狀況>一節。See the Exceptions section.

ignoreCase
Boolean Boolean Boolean Boolean

true 執行不區分大小寫的搜尋,則為 typeName;為 false 執行區分大小寫的搜尋,則為 typeNametrue to perform a case-insensitive search for typeName, false to perform a case-sensitive search for typeName.

傳回

具有指定名稱的類型。The type with the specified name. 如果找不到該類型,throwOnError 參數會指定是要傳回 null 或是擲回例外狀況。If the type is not found, the throwOnError parameter specifies whether null is returned or an exception is thrown. 在某些情況下,不論 throwOnError 的值為何,都會擲回例外狀況。In some cases, an exception is thrown regardless of the value of throwOnError. 請參閱<例外狀況>一節。See the Exceptions section.

例外狀況

叫用類別初始設定式並擲回例外狀況。A class initializer is invoked and throws an exception.

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

-或--or- throwOnErrortrue,而且 typeName 包含無效字元,例如內嵌的定位字元。throwOnError is true and typeName contains invalid characters, such as an embedded tab. -或--or- throwOnErrortrue,而且 typeName 是空字串。throwOnError is true and typeName is an empty string.

-或--or- throwOnErrortrue,而且 typeName 代表大小無效的陣列類型。throwOnError is true and typeName represents an array type with an invalid size.

-或--or- typeName 代表 TypedReference 的陣列。typeName represents an array of TypedReference.

throwOnErrortrue,而且 typeName 包含無效的語法。throwOnError is true and typeName contains invalid syntax. 例如,"MyType[,*,]"。For example, "MyType[,*,]".

-或--or- typeName 代表的泛型類型以指標類型、ByRef 類型或 Void 做為其中一個類型引數。typeName represents a generic type that has a pointer type, a ByRef type, or Void as one of its type arguments.

-或--or- typeName 代表的泛型類型具有不正確的類型引數數目。typeName represents a generic type that has an incorrect number of type arguments.

-或--or- typeName 代表泛型類型,其中一個類型引數不符合對應類型參數的條件約束。typeName represents a generic type, and one of its type arguments does not satisfy the constraints for the corresponding type parameter.

throwOnErrortrue,而且找不到組件或組件的其中一個相依性。throwOnError is true and the assembly or one of its dependencies was not found.

找到組件或其相依性的其中一個,但無法載入。The assembly or one of its dependencies was found, but could not be loaded.

組件或組件的其中一個相依性無效。The assembly or one of its dependencies is not valid.

-或--or- 目前載入的通用語言執行平台是 2.0 版或更新的版本,但用來編譯組件的卻是更新的版本。Version 2.0 or later of the common language runtime is currently loaded, and the assembly was compiled with a later version.

備註

您可以使用GetType方法,以取得Type另一個組件,如果您知道其組件限定的名稱,可以取得從這個型別的物件AssemblyQualifiedNameYou can use the GetType method to obtain a Type object for a type in another assembly if you know its assembly-qualified name, which can be obtained from AssemblyQualifiedName. GetType 會導致在指定的組件載入typeNameGetType causes loading of the assembly specified in typeName. 您也可以載入組件Assembly.Load方法,然後使用Assembly.GetType或是Assembly.GetTypes方法來取得Type物件。You can also load an assembly using the Assembly.Load method, and then use the Assembly.GetType or Assembly.GetTypes method to get Type objects. 如果類型是在編譯時期已知到您的程式組件中,會使用更有效率typeof在C#或GetType在 Visual Basic 中的運算子。If a type is in an assembly known to your program at compile time, it is more efficient to use typeof in C# or the GetType operator in Visual Basic.

GetType 僅適用於從磁碟載入的組件。GetType only works on assemblies loaded from disk. 如果您呼叫GetType查閱使用定義動態組件中定義的型別System.Reflection.Emit服務,您可能會收到不一致的行為。If you call GetType to look up a type defined in a dynamic assembly defined using the System.Reflection.Emit services, you might get inconsistent behavior. 行為取決於動態組件是持續性的也就是,使用建立RunAndSave或是Save存取模式的System.Reflection.Emit.AssemblyBuilderAccess列舉型別。The behavior depends on whether the dynamic assembly is persistent, that is, created using the RunAndSave or Save access modes of the System.Reflection.Emit.AssemblyBuilderAccess enumeration. 如果動態組件是持續性,並已寫入磁碟之前GetType是呼叫,載入器在磁碟上找到儲存的組件、 載入該組件中,而且該組件中擷取的型別。If the dynamic assembly is persistent and has been written to disk before GetType is called, the loader finds the saved assembly on disk, loads that assembly, and retrieves the type from that assembly. 如果組件尚未儲存至磁碟GetType呼叫時,此方法會傳回nullIf the assembly has not been saved to disk when GetType is called, the method returns null. GetType 不了解暫時性動態組件;因此,呼叫GetType若要擷取型別中的暫時性動態組件傳回nullGetType does not understand transient dynamic assemblies; therefore, calling GetType to retrieve a type in a transient dynamic assembly returns null.

若要使用GetType上的動態模組,訂閱AppDomain.AssemblyResolve事件,並呼叫GetType在儲存之前。To use GetType on a dynamic module, subscribe to the AppDomain.AssemblyResolve event and call GetType before saving. 否則,您會得到兩個組件的複本在記憶體中。Otherwise, you will get two copies of the assembly in memory.

throwOnError參數會指定時會發生什麼事的型別找不到,並會隱藏其他某些例外情況,例外狀況 」 一節中所述。The throwOnError parameter specifies what happens when the type is not found, and also suppresses certain other exception conditions, as described in the Exceptions section. 某些例外狀況的值為何throwOnErrorSome exceptions are thrown regardless of the value of throwOnError. 例如,如果找到的型別,但無法載入,TypeLoadException就會擲回即使throwOnErrorfalseFor example, if the type is found but cannot be loaded, a TypeLoadException is thrown even if throwOnError is false.

下表顯示基底類別成員會傳回由Get反映型別上時的方法。The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

成員類型Member Type StaticStatic 非靜態Non-Static
建構函式Constructor No No
欄位Field No 可以。Yes. 欄位永遠是依名稱和簽章隱藏。A field is always hide-by-name-and-signature.
Event - 事件Event 不適用Not applicable 一般類型系統規則是繼承實作屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會視為由名稱和簽章隱藏屬性。Reflection treats properties as hide-by-name-and-signature. 請參閱附註 2。See note 2 below.
方法Method No 可以。Yes. (虛擬和非虛擬) 的方法可以隱藏依名稱或依名稱和簽章隱藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
巢狀的類型Nested Type No No
屬性Property 不適用Not applicable 一般類型系統規則是繼承實作屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會視為由名稱和簽章隱藏屬性。Reflection treats properties as hide-by-name-and-signature. 請參閱附註 2。See note 2 below.
  1. 依名稱和簽章隱藏會考慮所有組件的簽章,包括自訂修飾詞,傳回類型、 參數類型、 sentinels 和 unmanaged 呼叫慣例。Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. 這是二進位比較。This is a binary comparison.

  2. 反映的屬性和事件會依名稱和簽章隱藏。For reflection, properties and events are hide-by-name-and-signature. 如果您同時有 get 和 set 存取子的屬性在基底類別中,但衍生的類別有只有 get 存取子,在衍生的類別的屬性會隱藏基底類別屬性中,並不能存取的基底類別的 setter。If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.

  3. 自訂屬性不屬於一般類型系統。Custom attributes are not part of the common type system.

陣列或 COM 型別不會搜尋除非它們已經被載入資料表可用的類別。Arrays or COM types are not searched for unless they have already been loaded into the table of available classes.

typeName 可以在型別名稱來限定其命名空間或組件限定名稱,其中包含組件名稱規格。typeName can be the type name qualified by its namespace or an assembly-qualified name that includes an assembly name specification. 請參閱 AssemblyQualifiedNameSee AssemblyQualifiedName.

如果typeName包含命名空間,但不是組件名稱,這個方法會依此順序搜尋只呼叫物件的組件和 Mscorlib.dll。If typeName includes the namespace but not the assembly name, this method searches only the calling object's assembly and Mscorlib.dll, in that order. 如果部分或完整的組件名稱完整限定類型名稱,這個方法會搜尋指定的組件中。If typeName is fully qualified with the partial or complete assembly name, this method searches in the specified assembly. 如果組件具有強式名稱,完整的組件的名稱是必要。If the assembly has a strong name, a complete assembly name is required.

AssemblyQualifiedName屬性會傳回完整的型別名稱,包括巢狀型別、 組件名稱和型別引數。The AssemblyQualifiedName property returns a fully qualified type name including nested types, the assembly name, and type arguments. 支援通用語言執行平台的所有編譯器將都發出的簡單名稱的巢狀類別,並反映建構查詢時,根據下列慣例 mangled 的名稱。All compilers that support the common language runtime will emit the simple name of a nested class, and reflection constructs a mangled name when queried, in accordance with the following conventions.

注意

在.NET Framework 2.0 版中,處理器架構會新增至組件識別,而且可以指定為組件名稱字串的一部分。In the .NET Framework version 2.0, processor architecture is added to assembly identity, and can be specified as part of assembly name strings. 例如,"ProcessorArchitecture = msil"。For example, "ProcessorArchitecture=msil". 不過,它不包含在所傳回的字串AssemblyQualifiedName屬性,因為相容性。However, it is not included in the string returned by the AssemblyQualifiedName property, for compatibility reasons. 您也可以藉由建立載入類型AssemblyName物件,並將其傳遞至適當的多載Load方法。You can also load types by creating an AssemblyName object and passing it to an appropriate overload of the Load method. 您可以接著使用Assembly.GetType方法來載入組件類型。You can then use the Assembly.GetType method to load types from the assembly. 請參閱 AssemblyName.ProcessorArchitectureSee also AssemblyName.ProcessorArchitecture.

分隔符號Delimiter 意義Meaning
反斜線 (\)Backslash (\) 逸出字元。Escape character.
倒單引號 (')Backtick (`) 前面有一或多個位數表示的型別參數,位於結尾的泛型型別名稱的數目。Precedes one or more digits representing the number of type parameters, located at the end of the name of a generic type.
括號 ([])Brackets ([]) 將建構的泛型類型; 泛型型別引數清單在類型引數清單中,將組件限定型別。Enclose a generic type argument list, for a constructed generic type; within a type argument list, enclose an assembly-qualified type.
逗號 (,)Comma (,) 後面接著組件名稱。Precedes the Assembly name.
句號 (.)Period (.) 表示命名空間識別項。Denotes namespace identifiers.
加號 (+)Plus sign (+) 在之前的巢狀的類別。Precedes a nested class.

例如,類別的完整格式的名稱可能如下所示:For example, the fully qualified name for a class might look like this:

TopNamespace.SubNameSpace.ContainingClass+NestedClass,MyAssembly  

如果命名空間已 TopNamespace.Sub+Namespace,則字串會有之前加號 (+),以逸出字元 (\) 以防止它被解譯為巢狀分隔符號。If the namespace were TopNamespace.Sub+Namespace, then the string would have to precede the plus sign (+) with an escape character (\) to prevent it from being interpreted as a nesting separator. 反映會發出此字串,如下所示:Reflection emits this string as follows:

TopNamespace.Sub\+Namespace.ContainingClass+NestedClass,MyAssembly  

"+ +"會變成"\+\+",和 「\」 變成 「\\"。A "++" becomes "\+\+", and a "\" becomes "\\".

這個限定的名稱可以保存和更新版本用來載入TypeThis qualified name can be persisted and later used to load the Type. 搜尋並載入Type,使用GetType使用型別名稱只有或使用組件限定的類型名稱。To search for and load a Type, use GetType either with the type name only or with the assembly qualified type name. GetType 與型別名稱只會尋找Type呼叫者的組件中,然後在系統組件中。GetType with the type name only will look for the Type in the caller's assembly and then in the System assembly. GetType 與組件限定的類型名稱會尋找Type任何組件中。GetType with the assembly qualified type name will look for the Type in any assembly.

類型名稱可能包含行尾字元來表示的類型,例如類型是否為參考型別、 指標類型或陣列類型的其他資訊。Type names may include trailing characters that denote additional information about the type, such as whether the type is a reference type, a pointer type or an array type. 若要擷取的型別名稱,而不需要這些後置字元,請使用t.GetElementType().ToString(),其中t是型別。To retrieve the type name without these trailing characters, use t.GetElementType().ToString(), where t is the type.

空格是相關的組件名稱以外的所有型別名稱元件。Spaces are relevant in all type name components except the assembly name. 在組件名稱 ',' 分隔符號之前的空格是相關的但是 ',' 分隔符號之後的空格會被忽略。In the assembly name, spaces before the ',' separator are relevant, but spaces after the ',' separator are ignored.

泛型型別名稱的結尾倒引號 (`) 後面接著數字,代表泛型類型引數數目。The name of a generic type ends with a backtick (`) followed by digits representing the number of generic type arguments. 此名稱的損害的目的是讓編譯器支援泛型型別具有相同名稱但具有不同數目的型別參數,在相同的範圍內發生。The purpose of this name mangling is to allow compilers to support generic types with the same name but with different numbers of type parameters, occurring in the same scope. 例如,反映會傳回損害的名稱Tuple1andTuple2的泛型方法Tuple(Of T)Tuple(Of T0, T1)在 Visual Basic 中,或Tuple<T>和 Tuple\<T0, T1> Visual C# 中。For example, reflection returns the mangled names Tuple1andTuple2 from the generic methods Tuple(Of T) and Tuple(Of T0, T1) in Visual Basic, or Tuple<T> and Tuple\<T0, T1> in Visual C#.

為泛型類型,類型引數清單以括號、,並以逗號分隔的類型引數。For generic types, the type argument list is enclosed in brackets, and the type arguments are separated by commas. 例如,泛型Dictionary<TKey,TValue>有兩個類型參數。For example, a generic Dictionary<TKey,TValue> has two type parameters. ADictionary<TKey,TValue>MyType類型的索引鍵聯String可能如下表示:A Dictionary<TKey,TValue> of MyType with keys of type String might be represented as follows:

System.Collections.Generic.Dictionary`2[System.String,MyType]  

若要指定型別引數清單中的組件限定型別,請將括號括住的組件限定型別。To specify an assembly-qualified type within a type argument list, enclose the assembly-qualified type within brackets. 否則,逗號分隔的組件限定名稱的組件會被視為分隔其他的型別引數。Otherwise, the commas that separate the parts of the assembly-qualified name are interpreted as delimiting additional type arguments. 例如,Dictionary<TKey,TValue>MyTypeMyAssembly.dll 中,使用索引鍵的類型從String,可能會以下面方式指定:For example, a Dictionary<TKey,TValue> of MyType from MyAssembly.dll, with keys of type String, might be specified as follows:

Type.GetType("System.Collections.Generic.Dictionary`2[System.String,[MyType,MyAssembly]]")  

注意

組件限定型別可以括在括弧,它必須出現在類型參數清單中。An assembly-qualified type can be enclosed in brackets only when it appears within a type parameter list. 搜尋組件限定的不合格的型別的型別參數清單中的規則並符合規定及不合格的非泛型類型的規則相同。The rules for searching assemblies for qualified and unqualified types in type parameter lists are the same as the rules for qualified and unqualified nongeneric types.

可為 null 的型別是泛型類型的特殊案例。Nullable types are a special case of generic types. 例如,可為 null Int32 "System.Nullable'1[System.Int32]"的字串表示。For example, a nullable Int32 is represented by the string "System.Nullable`1[System.Int32]".

注意

在C#, C++,以及 Visual Basic,您也可以取得可為 null 類型使用型別運算子。In C#, C++, and Visual Basic you can also get nullable types using type operators. 比方說,可為 nullBoolean型別由typeof(Nullable<bool>)在C#、 藉由Nullable<Boolean>::typeid中C++,並依據GetType(Nullable(Of Boolean))在 Visual Basic 中。For example, the nullable Boolean type is returned by typeof(Nullable<bool>) in C#, by Nullable<Boolean>::typeid in C++, and by GetType(Nullable(Of Boolean)) in Visual Basic.

下表顯示您所使用的語法GetType為各種型別。The following table shows the syntax you use with GetType for various types.

若要取得To Get 使用Use
可為 null Int32A nullable Int32 Type.GetType("System.Nullable`1[System.Int32]")
Unmanaged 的指標 MyTypeAn unmanaged pointer to MyType Type.GetType("MyType*")
指標的 unmanaged 的指標 MyTypeAn unmanaged pointer to a pointer to MyType Type.GetType("MyType**")
Managed 的指標或參考 MyTypeA managed pointer or reference to MyType Type.GetType("MyType&").Type.GetType("MyType&"). 請注意,參考與指標不同,僅限一個層級。Note that unlike pointers, references are limited to one level.
父類別和巢狀的類別A parent class and a nested class Type.GetType("MyParentClass+MyNestedClass")
一維陣列,其下限為 0A one-dimensional array with a lower bound of 0 Type.GetType("MyArray[]")
下限不明的一維陣列A one-dimensional array with an unknown lower bound Type.GetType("MyArray[*]")
N 維陣列An n-dimensional array 逗號 (,) 的 n-1 次數總計的方括號內。A comma (,) inside the brackets a total of n-1 times. 例如,System.Object[,,]代表三維Object陣列。For example, System.Object[,,] represents a three-dimensional Object array.
二維陣列的陣列A two-dimensional array's array Type.GetType("MyArray[][]")
矩形的二維陣列下限不明A rectangular two-dimensional array with unknown lower bounds Type.GetType("MyArray[,]")
具有一個型別引數的泛型型別A generic type with one type argument Type.GetType("MyGenericType`1[MyType]")
兩個類型引數的泛型型別A generic type with two type arguments Type.GetType("MyGenericType`2[MyType,AnotherType]")
具有兩個組件限定型別引數的泛型型別A generic type with two assembly-qualified type arguments Type.GetType("MyGenericType`2[[MyType,MyAssembly],[AnotherType,AnotherAssembly]]")
使用組件限定型別引數,組件限定的泛型類型An assembly-qualified generic type with an assembly-qualified type argument Type.GetType("MyGenericType`1[[MyType,MyAssembly]],MyGenericTypeAssembly")
其類型引數是具有兩個類型引數的泛型型別是泛型型別A generic type whose type argument is a generic type with two type arguments Type.GetType("MyGenericType`1[AnotherGenericType`2[MyType,AnotherType]]")
另請參閱

GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>)

取得具有指定名稱的類型,並選擇性提供自訂方法來解析組件和類型。Gets the type with the specified name, optionally providing custom methods to resolve the assembly and the type.

public:
 static Type ^ GetType(System::String ^ typeName, Func<System::Reflection::AssemblyName ^, System::Reflection::Assembly ^> ^ assemblyResolver, Func<System::Reflection::Assembly ^, System::String ^, bool, Type ^> ^ typeResolver);
public static Type GetType (string typeName, Func<System.Reflection.AssemblyName,System.Reflection.Assembly> assemblyResolver, Func<System.Reflection.Assembly,string,bool,Type> typeResolver);
static member GetType : string * Func<System.Reflection.AssemblyName, System.Reflection.Assembly> * Func<System.Reflection.Assembly, string, bool, Type> -> Type
Public Shared Function GetType (typeName As String, assemblyResolver As Func(Of AssemblyName, Assembly), typeResolver As Func(Of Assembly, String, Boolean, Type)) As Type

參數

typeName
String String String String

要取得之類型的名稱。The name of the type to get. 如果已提供 typeResolver 參數,則類型名稱可為 typeResolver 能解析的任何字串。If the typeResolver parameter is provided, the type name can be any string that typeResolver is capable of resolving. 如果已提供 assemblyResolver 參數或已使用標準類型解析,typeName 就必須是組件限定名稱 (請參閱 AssemblyQualifiedName),除非類型位於目前正在執行的組件或 Mscorlib.dll 中,這種情況時,此類型就能提供本身命名空間限定的類型名稱。If the assemblyResolver parameter is provided or if standard type resolution is used, typeName must be an assembly-qualified name (see AssemblyQualifiedName), unless the type is in the currently executing assembly or in Mscorlib.dll, in which case it is sufficient to supply the type name qualified by its namespace.

assemblyResolver
Func<AssemblyName,Assembly>

方法,會找出並傳回 typeName 中指定的組件。A method that locates and returns the assembly that is specified in typeName. 組件名稱做為 assemblyResolver 物件傳遞至 AssemblyNameThe assembly name is passed to assemblyResolver as an AssemblyName object. 如果 typeName 不包含組件名稱,則不會呼叫 assemblyResolverIf typeName does not contain the name of an assembly, assemblyResolver is not called. 如果未提供 assemblyResolver,則會執行標準組件解析。If assemblyResolver is not supplied, standard assembly resolution is performed.

注意:不要從未知或未受信任的呼叫端傳遞方法。Caution Do not pass methods from unknown or untrusted callers. 這樣做可能會提高惡意程式碼的權限。Doing so could result in elevation of privilege for malicious code. 只使用您所提供或熟悉的方法。Use only methods that you provide or that you are familiar with.

typeResolver
Func<Assembly,String,Boolean,Type>

typeName 或標準組件解析傳回的組件,找出並傳回由 assemblyResolver 指定之類型的方法。A method that locates and returns the type that is specified by typeName from the assembly that is returned by assemblyResolver or by standard assembly resolution. 如果未提供任何組件,typeResolver 方法會提供一個組件。If no assembly is provided, the typeResolver method can provide one. 這個方法也使用參數來指定是否執行不區分大小寫的搜尋。會傳遞 false 至該參數。The method also takes a parameter that specifies whether to perform a case-insensitive search; false is passed to that parameter.

注意:不要從未知或未受信任的呼叫端傳遞方法。Caution Do not pass methods from unknown or untrusted callers.

傳回

具有指定名稱的類型;如果找不到類型,則為 nullThe type with the specified name, or null if the type is not found.

例外狀況

叫用類別初始設定式並擲回例外狀況。A class initializer is invoked and throws an exception.

typeName 剖析成類型名稱和組件名稱 (例如簡單類型名稱包含未逸出的特殊字元) 時發生錯誤。An error occurs when typeName is parsed into a type name and an assembly name (for example, when the simple type name includes an unescaped special character).

-或--or- typeName 代表的泛型類型以指標類型、ByRef 類型或 Void 做為其中一個類型引數。typeName represents a generic type that has a pointer type, a ByRef type, or Void as one of its type arguments.

-或--or- typeName 代表的泛型類型具有不正確的類型引數數目。typeName represents a generic type that has an incorrect number of type arguments.

-或--or- typeName 代表泛型類型,其中一個類型引數不符合對應類型參數的條件約束。typeName represents a generic type, and one of its type arguments does not satisfy the constraints for the corresponding type parameter.

typeName 代表 TypedReference 的陣列。typeName represents an array of TypedReference.

找到組件或其相依性的其中一個,但無法載入。The assembly or one of its dependencies was found, but could not be loaded.

-或--or- typeName 包含無效組件名稱。typeName contains an invalid assembly name.

-或--or- typeName 是不具類型名稱的有效組件名稱。typeName is a valid assembly name without a type name.

組件或組件的其中一個相依性無效。The assembly or one of its dependencies is not valid.

-或--or- 用以編譯組件的通用語言執行平台,其版本比目前所載入的版本還要更新。The assembly was compiled with a later version of the common language runtime than the version that is currently loaded.

備註

這個方法和詳細的使用案例assemblyResolvertypeResolver參數可在GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean)方法多載。Usage scenarios for this method and details about the assemblyResolver and typeResolver parameters can be found in the GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) method overload.

注意

如果typeName找不到,呼叫GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>)方法會傳回nullIf typeName cannot be found, the call to the GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>) method returns null. 它不會擲回例外狀況。It does not throw an exception. 若要控制是否發生例外狀況,請呼叫的多載GetType方法具有throwOnError參數。To control whether an exception is thrown, call an overload of the GetType method that has a throwOnError parameter.

呼叫這個方法多載等同於呼叫GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean)方法多載,並指定falseforthrowOnErrorignoreCase參數。Calling this method overload is the same as calling the GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) method overload and specifying false for the throwOnError and ignoreCase parameters.

GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean)

取得具有指定名稱的類型,指定找不到類型時是否擲回例外狀況,同時選擇性提供自訂方法來解析組件和類型。Gets the type with the specified name, specifying whether to throw an exception if the type is not found, and optionally providing custom methods to resolve the assembly and the type.

public:
 static Type ^ GetType(System::String ^ typeName, Func<System::Reflection::AssemblyName ^, System::Reflection::Assembly ^> ^ assemblyResolver, Func<System::Reflection::Assembly ^, System::String ^, bool, Type ^> ^ typeResolver, bool throwOnError);
public static Type GetType (string typeName, Func<System.Reflection.AssemblyName,System.Reflection.Assembly> assemblyResolver, Func<System.Reflection.Assembly,string,bool,Type> typeResolver, bool throwOnError);
static member GetType : string * Func<System.Reflection.AssemblyName, System.Reflection.Assembly> * Func<System.Reflection.Assembly, string, bool, Type> * bool -> Type
Public Shared Function GetType (typeName As String, assemblyResolver As Func(Of AssemblyName, Assembly), typeResolver As Func(Of Assembly, String, Boolean, Type), throwOnError As Boolean) As Type

參數

typeName
String String String String

要取得之類型的名稱。The name of the type to get. 如果已提供 typeResolver 參數,則類型名稱可為 typeResolver 能解析的任何字串。If the typeResolver parameter is provided, the type name can be any string that typeResolver is capable of resolving. 如果已提供 assemblyResolver 參數或已使用標準類型解析,typeName 就必須是組件限定名稱 (請參閱 AssemblyQualifiedName),除非類型位於目前正在執行的組件或 Mscorlib.dll 中,這種情況時,此類型就能提供本身命名空間限定的類型名稱。If the assemblyResolver parameter is provided or if standard type resolution is used, typeName must be an assembly-qualified name (see AssemblyQualifiedName), unless the type is in the currently executing assembly or in Mscorlib.dll, in which case it is sufficient to supply the type name qualified by its namespace.

assemblyResolver
Func<AssemblyName,Assembly>

方法,會找出並傳回 typeName 中指定的組件。A method that locates and returns the assembly that is specified in typeName. 組件名稱做為 assemblyResolver 物件傳遞至 AssemblyNameThe assembly name is passed to assemblyResolver as an AssemblyName object. 如果 typeName 不包含組件名稱,則不會呼叫 assemblyResolverIf typeName does not contain the name of an assembly, assemblyResolver is not called. 如果未提供 assemblyResolver,則會執行標準組件解析。If assemblyResolver is not supplied, standard assembly resolution is performed.

注意:不要從未知或未受信任的呼叫端傳遞方法。Caution Do not pass methods from unknown or untrusted callers. 這樣做可能會提高惡意程式碼的權限。Doing so could result in elevation of privilege for malicious code. 只使用您所提供或熟悉的方法。Use only methods that you provide or that you are familiar with.

typeResolver
Func<Assembly,String,Boolean,Type>

typeName 或標準組件解析傳回的組件,找出並傳回由 assemblyResolver 指定之類型的方法。A method that locates and returns the type that is specified by typeName from the assembly that is returned by assemblyResolver or by standard assembly resolution. 如果未提供任何組件,方法會提供一個組件。If no assembly is provided, the method can provide one. 這個方法也使用參數來指定是否執行不區分大小寫的搜尋。會傳遞 false 至該參數。The method also takes a parameter that specifies whether to perform a case-insensitive search; false is passed to that parameter.

注意:不要從未知或未受信任的呼叫端傳遞方法。Caution Do not pass methods from unknown or untrusted callers.

throwOnError
Boolean Boolean Boolean Boolean

true 表示找不到該類型時擲回例外狀況,而 false 則表示傳回 nulltrue to throw an exception if the type cannot be found; false to return null. 指定 false 也會隱藏其他某些例外狀況條件,但並不是全部。Specifying false also suppresses some other exception conditions, but not all of them. 請參閱<例外狀況>一節。See the Exceptions section.

傳回

具有指定名稱的類型。The type with the specified name. 如果找不到該類型,throwOnError 參數會指定是要傳回 null 或是擲回例外狀況。If the type is not found, the throwOnError parameter specifies whether null is returned or an exception is thrown. 在某些情況下,不論 throwOnError 的值為何,都會擲回例外狀況。In some cases, an exception is thrown regardless of the value of throwOnError. 請參閱<例外狀況>一節。See the Exceptions section.

例外狀況

叫用類別初始設定式並擲回例外狀況。A class initializer is invoked and throws an exception.

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

-或--or- throwOnErrortrue,而且 typeName 包含無效字元,例如內嵌的定位字元。throwOnError is true and typeName contains invalid characters, such as an embedded tab. -或--or- throwOnErrortrue,而且 typeName 是空字串。throwOnError is true and typeName is an empty string.

-或--or- throwOnErrortrue,而且 typeName 代表大小無效的陣列類型。throwOnError is true and typeName represents an array type with an invalid size.

-或--or- typeName 代表 TypedReference 的陣列。typeName represents an array of TypedReference.

typeName 剖析成類型名稱和組件名稱 (例如簡單類型名稱包含未逸出的特殊字元) 時發生錯誤。An error occurs when typeName is parsed into a type name and an assembly name (for example, when the simple type name includes an unescaped special character).

-或--or- throwOnErrortrue,而且 typeName 包含無效語法 (例如 "MyType[,*,]")。throwOnError is true and typeName contains invalid syntax (for example, "MyType[,*,]").

-或--or- typeName 代表的泛型類型以指標類型、ByRef 類型或 Void 做為其中一個類型引數。typeName represents a generic type that has a pointer type, a ByRef type, or Void as one of its type arguments.

-或--or- typeName 代表的泛型類型具有不正確的類型引數數目。typeName represents a generic type that has an incorrect number of type arguments.

-或--or- typeName 代表泛型類型,其中一個類型引數不符合對應類型參數的條件約束。typeName represents a generic type, and one of its type arguments does not satisfy the constraints for the corresponding type parameter.

throwOnErrortrue,而且找不到組件或組件的其中一個相依性。throwOnError is true and the assembly or one of its dependencies was not found.

-或--or- typeName 包含無效組件名稱。typeName contains an invalid assembly name.

-或--or- typeName 是不具類型名稱的有效組件名稱。typeName is a valid assembly name without a type name.

找到組件或其相依性的其中一個,但無法載入。The assembly or one of its dependencies was found, but could not be loaded.

組件或組件的其中一個相依性無效。The assembly or one of its dependencies is not valid.

-或--or- 用以編譯組件的通用語言執行平台,其版本比目前所載入的版本還要更新。The assembly was compiled with a later version of the common language runtime than the version that is currently loaded.

備註

這個方法和詳細的使用案例assemblyResolvertypeResolver參數可在GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean)方法多載。Usage scenarios for this method and details about the assemblyResolver and typeResolver parameters can be found in the GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) method overload.

呼叫這個方法多載等同於呼叫GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean)方法多載,並指定falseignoreCase參數。Calling this method overload is the same as calling the GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) method overload and specifying false for the ignoreCase parameter.

GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean) GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean, Boolean)

取得具有指定名稱的類型,指定是否執行區分大小寫的搜尋以及找不到類型時是否擲回例外狀況,同時選擇性提供自訂方法來解析組件和類型。Gets the type with the specified name, specifying whether to perform a case-sensitive search and whether to throw an exception if the type is not found, and optionally providing custom methods to resolve the assembly and the type.

public:
 static Type ^ GetType(System::String ^ typeName, Func<System::Reflection::AssemblyName ^, System::Reflection::Assembly ^> ^ assemblyResolver, Func<System::Reflection::Assembly ^, System::String ^, bool, Type ^> ^ typeResolver, bool throwOnError, bool ignoreCase);
public static Type GetType (string typeName, Func<System.Reflection.AssemblyName,System.Reflection.Assembly> assemblyResolver, Func<System.Reflection.Assembly,string,bool,Type> typeResolver, bool throwOnError, bool ignoreCase);
static member GetType : string * Func<System.Reflection.AssemblyName, System.Reflection.Assembly> * Func<System.Reflection.Assembly, string, bool, Type> * bool * bool -> Type
Public Shared Function GetType (typeName As String, assemblyResolver As Func(Of AssemblyName, Assembly), typeResolver As Func(Of Assembly, String, Boolean, Type), throwOnError As Boolean, ignoreCase As Boolean) As Type

參數

typeName
String String String String

要取得之類型的名稱。The name of the type to get. 如果已提供 typeResolver 參數,則類型名稱可為 typeResolver 能解析的任何字串。If the typeResolver parameter is provided, the type name can be any string that typeResolver is capable of resolving. 如果已提供 assemblyResolver 參數或已使用標準類型解析,typeName 就必須是組件限定名稱 (請參閱 AssemblyQualifiedName),除非類型位於目前正在執行的組件或 Mscorlib.dll 中,這種情況時,此類型就能提供本身命名空間限定的類型名稱。If the assemblyResolver parameter is provided or if standard type resolution is used, typeName must be an assembly-qualified name (see AssemblyQualifiedName), unless the type is in the currently executing assembly or in Mscorlib.dll, in which case it is sufficient to supply the type name qualified by its namespace.

assemblyResolver
Func<AssemblyName,Assembly>

方法,會找出並傳回 typeName 中指定的組件。A method that locates and returns the assembly that is specified in typeName. 組件名稱做為 assemblyResolver 物件傳遞至 AssemblyNameThe assembly name is passed to assemblyResolver as an AssemblyName object. 如果 typeName 不包含組件名稱,則不會呼叫 assemblyResolverIf typeName does not contain the name of an assembly, assemblyResolver is not called. 如果未提供 assemblyResolver,則會執行標準組件解析。If assemblyResolver is not supplied, standard assembly resolution is performed.

注意:不要從未知或未受信任的呼叫端傳遞方法。Caution Do not pass methods from unknown or untrusted callers. 這樣做可能會提高惡意程式碼的權限。Doing so could result in elevation of privilege for malicious code. 只使用您所提供或熟悉的方法。Use only methods that you provide or that you are familiar with.

typeResolver
Func<Assembly,String,Boolean,Type>

typeName 或標準組件解析傳回的組件,找出並傳回由 assemblyResolver 指定之類型的方法。A method that locates and returns the type that is specified by typeName from the assembly that is returned by assemblyResolver or by standard assembly resolution. 如果未提供任何組件,方法會提供一個組件。If no assembly is provided, the method can provide one. 這個方法也使用參數來指定是否執行不區分大小寫的搜尋。會傳遞 ignoreCase 的值至該參數。The method also takes a parameter that specifies whether to perform a case-insensitive search; the value of ignoreCase is passed to that parameter.

注意:不要從未知或未受信任的呼叫端傳遞方法。Caution Do not pass methods from unknown or untrusted callers.

throwOnError
Boolean Boolean Boolean Boolean

true 表示找不到該類型時擲回例外狀況,而 false 則表示傳回 nulltrue to throw an exception if the type cannot be found; false to return null. 指定 false 也會隱藏其他某些例外狀況條件,但並不是全部。Specifying false also suppresses some other exception conditions, but not all of them. 請參閱<例外狀況>一節。See the Exceptions section.

ignoreCase
Boolean Boolean Boolean Boolean

true 執行不區分大小寫的搜尋,則為 typeName;為 false 執行區分大小寫的搜尋,則為 typeNametrue to perform a case-insensitive search for typeName, false to perform a case-sensitive search for typeName.

傳回

具有指定名稱的類型。The type with the specified name. 如果找不到該類型,throwOnError 參數會指定是要傳回 null 或是擲回例外狀況。If the type is not found, the throwOnError parameter specifies whether null is returned or an exception is thrown. 在某些情況下,不論 throwOnError 的值為何,都會擲回例外狀況。In some cases, an exception is thrown regardless of the value of throwOnError. 請參閱<例外狀況>一節。See the Exceptions section.

例外狀況

叫用類別初始設定式並擲回例外狀況。A class initializer is invoked and throws an exception.

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

-或--or- throwOnErrortrue,而且 typeName 包含無效字元,例如內嵌的定位字元。throwOnError is true and typeName contains invalid characters, such as an embedded tab. -或--or- throwOnErrortrue,而且 typeName 是空字串。throwOnError is true and typeName is an empty string.

-或--or- throwOnErrortrue,而且 typeName 代表大小無效的陣列類型。throwOnError is true and typeName represents an array type with an invalid size.

-或--or- typeName 代表 TypedReference 的陣列。typeName represents an array of TypedReference.

typeName 剖析成類型名稱和組件名稱 (例如簡單類型名稱包含未逸出的特殊字元) 時發生錯誤。An error occurs when typeName is parsed into a type name and an assembly name (for example, when the simple type name includes an unescaped special character).

-或--or- throwOnErrortrue,而且 typeName 包含無效語法 (例如 "MyType[,*,]")。throwOnError is true and typeName contains invalid syntax (for example, "MyType[,*,]").

-或--or- typeName 代表的泛型類型以指標類型、ByRef 類型或 Void 做為其中一個類型引數。typeName represents a generic type that has a pointer type, a ByRef type, or Void as one of its type arguments.

-或--or- typeName 代表的泛型類型具有不正確的類型引數數目。typeName represents a generic type that has an incorrect number of type arguments.

-或--or- typeName 代表泛型類型,其中一個類型引數不符合對應類型參數的條件約束。typeName represents a generic type, and one of its type arguments does not satisfy the constraints for the corresponding type parameter.

throwOnErrortrue,而且找不到組件或組件的其中一個相依性。throwOnError is true and the assembly or one of its dependencies was not found.

找到組件或其相依性的其中一個,但無法載入。The assembly or one of its dependencies was found, but could not be loaded.

-或--or- typeName 包含無效組件名稱。typeName contains an invalid assembly name.

-或--or- typeName 是不具類型名稱的有效組件名稱。typeName is a valid assembly name without a type name.

組件或組件的其中一個相依性無效。The assembly or one of its dependencies is not valid.

-或--or- 用以編譯組件的通用語言執行平台,其版本比目前所載入的版本還要更新。The assembly was compiled with a later version of the common language runtime than the version that is currently loaded.

備註

使用這個方法多載和其相關聯的多載 (GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>)GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean)) 來取代的預設實作GetType具有更具彈性的實作方法。Use this method overload and its associated overloads (GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>) and GetType(String, Func<AssemblyName,Assembly>, Func<Assembly,String,Boolean,Type>, Boolean)) to replace the default implementation of the GetType method with more flexible implementations. 藉由提供您自己解決類型名稱和包含它們的組件名稱的方法,您可以執行下列作業:By providing your own methods that resolve type names and the names of the assemblies that contain them, you can do the following:

  • 控制型別會從載入的組件的版本。Control which version of an assembly a type is loaded from.

  • 提供了另一個位置尋找不包含組件名稱的型別名稱。Provide another place to look for a type name that does not include an assembly name.

  • 使用部分組件名稱的組件載入。Load assemblies using partial assembly names.

  • 傳回的子類別System.Type,不由 common language runtime (CLR) 建立。Return subclasses of System.Type that are not created by the common language runtime (CLR).

比方說,在版本相容序列化這個方法可讓您使用的部分名稱來搜尋"best fit"的組件。For example, in version-tolerant serialization this method enables you to search for a "best fit" assembly by using a partial name. 其他多載GetType方法需要的組件限定類型名稱,其中包含的版本號碼。Other overloads of the GetType method require an assembly-qualified type name, which includes the version number.

型別系統的替代實作可能要傳回的子System.Type,不會建立由 CLR; 所有類型的其他多載會傳回GetType方法都是執行階段型別。Alternate implementations of the type system may need to return subclasses of System.Type that are not created by the CLR; all types that are returned by other overloads of the GetType method are runtime types.

使用方式附註Usage Notes

這個方法多載和其相關聯的多載剖析typeName至型別和組件名稱的名稱,然後解析名稱。This method overload and its associated overloads parse typeName into the name of a type and the name of an assembly, and then resolve the names. 解析的型別名稱之前, 的事件,是因為型別名稱必須解析組件的內容中發生的組件名稱的解析。Resolution of the assembly name occurs before resolution of the type name, because a type name must be resolved in the context of an assembly.

注意

如果您熟悉管理組件限定類型名稱的概念,請參閱AssemblyQualifiedName屬性。If you are unfamiliar with the concept of assembly-qualified type names, see the AssemblyQualifiedName property.

如果typeName不是組件限定名稱,就會略過組件解析。If typeName is not an assembly-qualified name, assembly resolution is skipped. 可以 Mscorlib.dll 或目前正在執行的組件的內容中解析不合格的類型名稱,或您可以選擇性地提供中的組件typeResolver參數。Unqualified type names can be resolved in the context of Mscorlib.dll or the currently executing assembly, or you can optionally provide an assembly in the typeResolver parameter. 要包含或省略的組件名稱,不同類型的名稱解析會以表格形式顯示的效果混合的名稱解析一節。The effects of including or omitting the assembly name for different kinds of name resolution are displayed as a table in the Mixed Name Resolution section.

一般使用方式附註:General usage notes:

  • 不要傳遞至方法assemblyResolvertypeResolver如果它們是來自未知或不受信任呼叫端。Do not pass methods to assemblyResolver or typeResolver if they come from unknown or untrusted callers. 只使用您所提供或熟悉的方法。Use only methods that you provide or that you are familiar with.

    警告

    使用來自未知或不受信任呼叫端的方法,可能會造成提高權限的惡意程式碼。Using methods from unknown or untrusted callers could result in elevation of privilege for malicious code.

  • 如果您省略assemblyResolver及/或typeResolver參數、 值throwOnError參數傳遞至此執行預設的解析的方法。If you omit the assemblyResolver and/or typeResolver parameters, the value of the throwOnError parameter is passed to the methods that perform the default resolution.

  • 如果throwOnErrortrue,這個方法會擲回TypeLoadExceptiontypeResolver會傳回null,和FileNotFoundExceptionassemblyResolver傳回nullIf throwOnError is true, this method throws a TypeLoadException when typeResolver returns null, and a FileNotFoundException when assemblyResolver returns null.

  • 這個方法不會攔截擲回例外狀況assemblyResolvertypeResolverThis method does not catch exceptions thrown by assemblyResolver and typeResolver. 您負責的解析程式方法所擲回任何例外狀況。You are responsible for any exceptions that are thrown by the resolver methods.

解析組件Resolving Assemblies

assemblyResolver方法會接收AssemblyName物件,它會藉由剖析字串組件名稱包含在產生typeNameThe assemblyResolver method receives an AssemblyName object, which is produced by parsing the string assembly name that is included in typeName. 如果typeName不包含組件名稱,assemblyResolver就不會呼叫並null傳遞給typeResolverIf typeName does not contain an assembly name, assemblyResolver is not called and null is passed to typeResolver.

如果assemblyResolver不提供標準的組件探查用來尋找組件。If assemblyResolver is not supplied, standard assembly probing is used to locate the assembly. 如果assemblyResolver提供,則GetType方法不會執行標準探查; 在此情況下,您必須確定,您assemblyResolver可以處理您傳遞給它的所有組件。If assemblyResolver is provided, the GetType method does not do standard probing; in that case you must ensure that your assemblyResolver can handle all the assemblies you pass to it.

assemblyResolver方法應傳回null如果無法解析組件。The assemblyResolver method should return null if the assembly cannot be resolved. 如果assemblyResolver會傳回nulltypeResolver就不會呼叫並不進行任何處理就會發生; 此外,如果throwOnErrortrueFileNotFoundException就會擲回。If assemblyResolver returns null, typeResolver is not called and no further processing occurs; additionally, if throwOnError is true, a FileNotFoundException is thrown.

如果AssemblyName傳遞至assemblyResolver是部分名稱、 一或多個其組件是nullIf the AssemblyName that is passed to assemblyResolver is a partial name, one or more of its parts are null. 例如,如果不有任何版本中,Version屬性是nullFor example, if it has no version, the Version property is null. 如果Version屬性,CultureInfo屬性,而GetPublicKeyToken方法都會傳回null,然後提供組件的簡單名稱。If the Version property, the CultureInfo property, and the GetPublicKeyToken method all return null, then only the simple name of the assembly was supplied. assemblyResolver方法可以使用或忽略所有組件的組件名稱。The assemblyResolver method can use or ignore all parts of the assembly name.

不同的組件解析選項的效果會顯示以表格形式混合的名稱解析區段中的,針對簡單和組件限定類型名稱。The effects of different assembly resolution options are displayed as a table in the Mixed Name Resolution section, for simple and assembly-qualified type names.

解析型別Resolving Types

如果typeName未指定組件名稱,typeResolver一定會呼叫。If typeName does not specify an assembly name, typeResolver is always called. 如果typeName指定組件名稱,typeResolver只有成功解析組件名稱時,才會呼叫。If typeName specifies an assembly name, typeResolver is called only when the assembly name is successfully resolved. 如果assemblyResolver或 探查傳回的標準組件nulltypeResolver就不會呼叫。If assemblyResolver or standard assembly probing returns null, typeResolver is not called.

typeResolver方法接收的三個引數:The typeResolver method receives three arguments:

  • 要搜尋的組件或null如果typeName不包含組件名稱。The assembly to search or null if typeName does not contain an assembly name.

  • 簡單類型的名稱。The simple name of the type. 當巢狀型別,這是最外層包含型別。In the case of a nested type, this is the outermost containing type. 當泛型型別,這是泛型類型的簡單名稱。In the case of a generic type, this is the simple name of the generic type.

  • 布林值,是true型別名稱的大小寫是否要忽略。A Boolean value that is true if the case of type names is to be ignored.

實作會判定這些引數使用的方式。The implementation determines the way these arguments are used. typeResolver方法應傳回null若無法解析型別。The typeResolver method should return null if it cannot resolve the type. 如果typeResolver會傳回nullthrowOnErrortrue的這個多載GetType就會擲回TypeLoadExceptionIf typeResolver returns null and throwOnError is true, this overload of GetType throws a TypeLoadException.

不同的型別解析選項的效果會顯示以表格形式混合的名稱解析區段中的,針對簡單和組件限定類型名稱。The effects of different type resolution options are displayed as a table in the Mixed Name Resolution section, for simple and assembly-qualified type names.

解析巢狀型別Resolving Nested Types

如果typeName是巢狀的類型,而只有名稱的最外層包含型別傳遞至typeResolverIf typeName is a nested type, only the name of the outermost containing type is passed to typeResolver. typeResolver會傳回這個型別,GetNestedType方法會呼叫遞迴,直到最內層的巢狀型別已解決。When typeResolver returns this type, the GetNestedType method is called recursively until the innermost nested type has been resolved.

解析泛型型別Resolving Generic Types

GetType稱為遞迴運作來解決泛型型別:首先要解決的泛型型別本身,然後解決它的型別引數。The GetType is called recursively to resolve generic types: First to resolve the generic type itself, and then to resolve its type arguments. 如果型別引數是泛型,GetType稱為遞迴運作來解決其型別引數,等等。If a type argument is generic, GetType is called recursively to resolve its type arguments, and so on.

組合assemblyResolvertypeResolver您提供必須能夠解析這類遞迴的所有層級。The combination of assemblyResolver and typeResolver that you provide must be capable of resolving all levels of this recursion. 例如,假設您提供assemblyResolver控制載入MyAssemblyFor example, suppose you supply an assemblyResolver that controls the loading of MyAssembly. 假設您想要解析的泛型型別Dictionary<string, MyType>(Dictionary(Of String, MyType) Visual Basic 中)。Suppose you want to resolve the generic type Dictionary<string, MyType> (Dictionary(Of String, MyType) in Visual Basic). 您可能會傳遞下列泛型型別名稱:You might pass the following generic type name:

"System.Collections.Generic.Dictionary`2[System.String,[MyNamespace.MyType, MyAssembly]]"  

請注意,MyType是只有組件限定型別引數。Notice that MyType is the only assembly-qualified type argument. 名稱Dictionary<TKey,TValue>String類別不是組件限定。The names of the Dictionary<TKey,TValue> and String classes are not assembly-qualified. typeResolver必須是其中任一組件無法控制代碼或null,因為它會接收nullDictionary<TKey,TValue>StringYour typeResolver must be able handle either an assembly or null, because it will receive null for Dictionary<TKey,TValue> and String. 它可以處理這種情況下,藉由呼叫的多載GetType取用一個字串,因為這兩個非限定的類型名稱是在 Mscorlib.dll 中的方法:It can handle that case by calling an overload of the GetType method that takes a string, because both of the unqualified type names are in Mscorlib.dll:

Type t = Type.GetType(test,
                      (aName) => aName.Name == "MyAssembly" ? 
                          Assembly.LoadFrom(@".\MyPath\v5.0\MyAssembly.dll") : null,
                      (assem, name, ignore) => assem == null ? 
                          Type.GetType(name, false, ignore) : 
                              assem.GetType(name, false, ignore)
                     ); 

assemblyResolver不會呼叫方法的字典類型和字串類型,因為這些型別名稱不是組件限定。The assemblyResolver method is not called for the dictionary type and the string type, because those type names are not assembly-qualified.

現在假設,而不是System.String,第一個泛型引數類型是YourType,從YourAssembly:Now suppose that instead of System.String, the first generic argument type is YourType, from YourAssembly:

"System.Collections.Generic.Dictionary`2[[YourNamespace.YourType, YourAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null], [MyNamespace.MyType, MyAssembly]]"  

因為此組件 Mscorlib.dll 或目前正在執行的組件都不,您不能解決YourType不含組件限定名稱。Because this assembly is neither Mscorlib.dll nor the currently executing assembly, you cannot resolve YourType without an assembly-qualified name. 因為您assemblyResolve會遞迴呼叫,它必須能夠處理這種情況。Because your assemblyResolve will be called recursively, it must be able to handle this case. 而不是傳回null以外的組件MyAssembly,它現在會執行使用所提供的組件載入AssemblyName物件。Instead of returning null for assemblies other than MyAssembly, it now performs an assembly load using the supplied AssemblyName object.

Type t2 = Type.GetType(test,
                       (aName) => aName.Name == "MyAssembly" ? 
                           Assembly.LoadFrom(@".\MyPath\v5.0\MyAssembly.dll") : 
                           Assembly.Load(aName),
                       (assem, name, ignore) => assem == null ? 
                           Type.GetType(name, false, ignore) : 
                               assem.GetType(name, false, ignore), true
                      ); 

回到使用方式附註Back to Usage Notes.

解析含有特殊字元的類型名稱Resolving Type Names with Special Characters

某些字元在組件限定名稱中具有特殊意義。Certain characters have special meanings in assembly-qualified names. 如果是簡單類型名稱會包含這些字元,字元的簡單名稱組件限定名稱的一部分時,就會導致剖析錯誤。If a simple type name contains these characters, the characters cause parsing errors when the simple name is part of an assembly-qualified name. 若要避免發生剖析錯誤,您必須逸出特殊字元加上反斜線之前您可以將傳遞的組件限定名稱GetType方法。To avoid the parsing errors, you must escape the special characters with a backslash before you can pass the assembly-qualified name to the GetType method. 例如,如果名為的型別Strange]Type,逸出字元必須加入括號前面,如下所示: Strange\]TypeFor example, if a type is named Strange]Type, the escape character must be added ahead of the square bracket as follows: Strange\]Type.

注意

無法在 Visual Basic 或 C# 中,建立含有這類特殊字元的名稱,但可由使用 Microsoft intermediate language (MSIL),或發出動態組件。Names with such special characters cannot be created in Visual Basic or C#, but can be created by using Microsoft intermediate language (MSIL) or by emitting dynamic assemblies.

下表顯示型別名稱的特殊字元。The following table shows the special characters for type names.

字元Character 意義Meaning
, (逗號), (comma) 組件限定名稱的分隔符號。Delimiter for assembly-qualified names.
[] (方括弧)[] (square brackets) 為後置詞組,表示陣列型別;分隔符號組中,為封入泛型引數清單和組件限定名稱。As a suffix pair, indicates an array type; as a delimiter pair, encloses generic argument lists and assembly-qualified names.
& (連字號)& (ampersand) 當做尾碼,表示型別是參考型別。As a suffix, indicates that a type is a reference type.
* (星號)* (asterisk) 做為尾碼,表示為型別是指標類型。As a suffix, indicates that a type is a pointer type.
+ (加號)+ (plus) 巢狀類型的分隔符號。Delimiter for nested types.
\ (反斜線)\ (backslash) 逸出字元。Escape character.

屬性,例如AssemblyQualifiedName傳回正確逸出字串。Properties such as AssemblyQualifiedName return correctly escaped strings. 您必須傳遞正確逸出的字串GetType方法。You must pass correctly escaped strings to the GetType method. 依次GetType方法會傳遞到的正確逸出的名稱typeResolver以及預設型別解析方法。In turn, the GetType method passes correctly escaped names to typeResolver and to the default type resolution methods. 如果您要比較的名稱中的未逸出名稱typeResolver,您必須先移除逸出字元。If you need to compare a name to an unescaped name in typeResolver, you must remove the escape characters.

回到使用方式附註Back to Usage Notes.

混合的名稱解析Mixed Name Resolution

下表摘要說明之間的互動assemblyResolvertypeResolver,和預設名稱解析的型別名稱和組件名稱中的所有組合typeName:The following table summarizes the interactions between assemblyResolver, typeResolver, and default name resolution, for all combinations of type name and assembly name in typeName:

型別名稱的內容Contents of type name 組件解析程式方法Assembly resolver method 類型解析程式方法Type resolver method 結果Result
型別,組件type, assembly nullnull nullnull 相當於呼叫Type.GetType(String, Boolean, Boolean)方法多載。Equivalent to calling the Type.GetType(String, Boolean, Boolean) method overload.
型別,組件type, assembly 提供provided nullnull assemblyResolver 傳回組件,則傳回null若無法解析組件。assemblyResolver returns the assembly or returns null if it cannot resolve the assembly. 如果組件已解決,Assembly.GetType(String, Boolean, Boolean)方法多載用來從組件載入的型別; 否則就不會嘗試解析的型別。If the assembly is resolved, the Assembly.GetType(String, Boolean, Boolean) method overload is used to load the type from the assembly; otherwise, there is no attempt to resolve the type.
型別,組件type, assembly nullnull 提供provided 相當於將轉換的組件名稱AssemblyName物件並呼叫Assembly.Load(AssemblyName)方法多載,以取得組件。Equivalent to converting the assembly name to an AssemblyName object and calling the Assembly.Load(AssemblyName) method overload to get the assembly. 如果組件已解決,請將它傳遞給typeResolver; 否則即為typeResolver就不會呼叫,而且沒有任何進一步嘗試解析的型別。If the assembly is resolved, it is passed to typeResolver; otherwise, typeResolver is not called and there is no further attempt to resolve the type.
型別,組件type, assembly 提供provided 提供provided assemblyResolver 傳回組件,則傳回null若無法解析組件。assemblyResolver returns the assembly or returns null if it cannot resolve the assembly. 如果組件已解決,請將它傳遞給typeResolver; 否則即為typeResolver就不會呼叫,而且沒有任何進一步嘗試解析的型別。If the assembly is resolved, it is passed to typeResolver; otherwise, typeResolver is not called and there is no further attempt to resolve the type.
類型type null、 提供null, provided nullnull 相當於呼叫Type.GetType(String, Boolean, Boolean)方法多載。Equivalent to calling the Type.GetType(String, Boolean, Boolean) method overload. 因為未提供組件名稱,就會搜尋只有 Mscorlib.dll 和目前執行的組件。Because the assembly name is not provided, only Mscorlib.dll and the currently executing assembly are searched. 如果assemblyResolver提供,則會忽略它。If assemblyResolver is provided, it is ignored.
類型type null、 提供null, provided 提供provided typeResolver 呼叫時,和null傳遞組件。typeResolver is called, and null is passed for the assembly. typeResolver 可以提供任何組件,包括它的目的載入的組件中的型別。typeResolver can provide a type from any assembly, including assemblies it loads for the purpose. 如果assemblyResolver提供,則會忽略它。If assemblyResolver is provided, it is ignored.
組件assembly null、 提供null, provided null、 提供null, provided AFileLoadException擲回,因為組件名稱會剖析一樣的組件限定類型名稱。A FileLoadException is thrown, because the assembly name is parsed as if it were an assembly-qualified type name. 這會導致無效的組件名稱。This results in an invalid assembly name.

取代為:使用方式附註解析組件解析型別Back to: Usage Notes, Resolving Assemblies, Resolving Types.

適用於