Type.GetNestedType 方法

定義

讓特定類型在目前的 Type 內變成巢狀。Gets a specific type nested within the current Type.

多載

GetNestedType(String, BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束搜尋指定的巢狀類型。When overridden in a derived class, searches for the specified nested type, using the specified binding constraints.

GetNestedType(String)

搜尋具有指定名稱的公用巢狀類型。Searches for the public nested type with the specified name.

GetNestedType(String, BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束搜尋指定的巢狀類型。When overridden in a derived class, searches for the specified nested type, using the specified binding constraints.

public:
 abstract Type ^ GetNestedType(System::String ^ name, System::Reflection::BindingFlags bindingAttr);
public abstract Type GetNestedType (string name, System.Reflection.BindingFlags bindingAttr);
abstract member GetNestedType : string * System.Reflection.BindingFlags -> Type
Public MustOverride Function GetNestedType (name As String, bindingAttr As BindingFlags) As Type

參數

name
String

包含要取得之巢狀類型名稱的字串。The string containing the name of the nested type to get.

bindingAttr
BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。A bitwise combination of the enumeration values that specify how the search is conducted.

-或--or- 要傳回 DefaultnullDefault to return null.

傳回

物件,代表符合指定之需求的巢狀類型 (如有找到),否則為 nullAn object representing the nested type that matches the specified requirements, if found; otherwise, null.

實作

例外狀況

namenullname is null.

備註

針對 name使用嵌套類別的簡單名稱。Use the simple name of the nested class for name. 請勿使用外部類別的名稱來限定它。Do not qualify it with the name of the outer class. 若為泛型嵌套類別,請使用損壞的名稱,也就是附加抑音符號和泛型參數的數目。For a generic nested class, use the mangled name - that is, append a grave accent and the number of generic parameters. 例如,使用字串 "Inner`1" 取得泛型嵌套類別 Inner<T>Inner(Of T) 在 Visual Basic 中)。For example, use the string "Inner`1" to get the generic nested class Inner<T> (Inner(Of T) in Visual Basic). 請勿包含型別參數的特定語言語法。Do not include language-specific syntax for type parameters.

下列 BindingFlags 篩選準則旗標可以用來定義要包含在搜尋中的巢狀型別:The following BindingFlags filter flags can be used to define which nested types to include in the search:

這個方法只會傳回目前類型的巢狀型別。This method returns only the nested types of the current type. 它不會搜尋目前類型的基類。It does not search the base classes of the current type. 若要尋找內嵌于基類中的類型,您必須將每個層級的 GetNestedType,逐一進行繼承階層。To find types that are nested in base classes, you must walk the inheritance hierarchy, calling GetNestedType at each level.

BindingFlags.InstanceBindingFlags.Static 會被忽略。BindingFlags.Instance and BindingFlags.Static are ignored.

只要使用 BindingFlags.Public 旗標或只有 BindingFlags.NonPublic 旗標呼叫此方法,就會傳回指定的巢狀型別,而且不需要任何其他旗標。Calling this method with only the BindingFlags.Public flag or only the BindingFlags.NonPublic flag will return the specified nested types and does not require any other flags.

如需相關資訊,請參閱 System.Reflection.BindingFlagsSee System.Reflection.BindingFlags for more information.

如果目前 Type 表示泛型型別或泛型方法定義中的類型參數,這個方法會搜尋類別條件約束的巢狀型別。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the nested types of the class constraint.

如果嵌套的類型是泛型,則這個方法會傳回它的泛型型別定義。If a nested type is generic, this method returns its generic type definition. 即使封閉式泛型型別是封閉的結構化型別,也是如此。This is true even if the enclosing generic type is a closed constructed type.

注意

如果目前的 Type 代表在、Visual Basic 或C# C++中定義的泛型型別,則其巢狀型別全都是泛型,即使它們本身沒有泛型參數也一樣。If the current Type represents a generic type defined in C#, Visual Basic, or C++, its nested types are all generic even if they have no generic parameters of their own. 這不一定適用于動態元件中定義的巢狀型別,或以eseutil.exe (IL組合器)編譯的。This is not necessarily true of nested types defined in dynamic assemblies or compiled with the Ilasm.exe (IL Assembler).

如需有關嵌套泛型型別,以及從其泛型型別定義來建立嵌套泛型型別的詳細資訊,請參閱 MakeGenericTypeFor information on nested generic types, and on constructing nested generic types from their generic type definitions, see MakeGenericType.

另請參閱

GetNestedType(String)

搜尋具有指定名稱的公用巢狀類型。Searches for the public nested type with the specified name.

public:
 Type ^ GetNestedType(System::String ^ name);
public:
 virtual Type ^ GetNestedType(System::String ^ name);
public Type GetNestedType (string name);
member this.GetNestedType : string -> Type
abstract member GetNestedType : string -> Type
override this.GetNestedType : string -> Type
Public Function GetNestedType (name As String) As Type

參數

name
String

包含要取得之巢狀類型名稱的字串。The string containing the name of the nested type to get.

傳回

物件,代表具有指定名稱的巢狀類型 (如有找到),否則為 nullAn object representing the public nested type with the specified name, if found; otherwise, null.

實作

例外狀況

namenullname is null.

備註

name 的搜尋會區分大小寫。The search for name is case-sensitive.

針對 name使用嵌套類別的簡單名稱。Use the simple name of the nested class for name. 請勿使用外部類別的名稱來限定它。Do not qualify it with the name of the outer class. 若為泛型嵌套類別,請使用損壞的名稱,也就是附加抑音符號和泛型引數的數目。For a generic nested class, use the mangled name - that is, append a grave accent and the number of generic arguments. 例如,使用字串 "Inner`1" 取得泛型嵌套類別 Inner<T>Inner(Of T) 在 Visual Basic 中)。For example, use the string "Inner`1" to get the generic nested class Inner<T> (Inner(Of T) in Visual Basic). 請勿包含型別參數的特定語言語法。Do not include language-specific syntax for type parameters.

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

成員類型Member Type 靜態Static 非靜態Non-Static
建構函式Constructor No No
欄位Field No 是。Yes. 欄位一律會依名稱和簽章隱藏。A field is always hide-by-name-and-signature.
事件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 和非受控呼叫慣例。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.

如果目前 Type 表示泛型型別或泛型方法定義中的類型參數,這個方法會搜尋類別條件約束的巢狀型別。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the nested types of the class constraint.

如果嵌套的類型是泛型,則這個方法會傳回它的泛型型別定義。If a nested type is generic, this method returns its generic type definition. 即使封閉式泛型型別是封閉的結構化型別,也是如此。This is true even if the enclosing generic type is a closed constructed type.

注意

如果目前的 Type 代表在、Visual Basic 或C# C++中定義的泛型型別,則其巢狀型別全都是泛型,即使它們本身沒有泛型參數也一樣。If the current Type represents a generic type defined in C#, Visual Basic, or C++, its nested types are all generic even if they have no generic parameters of their own. 這不一定適用于動態元件中定義的巢狀型別,或以eseutil.exe (IL組合器)編譯的。This is not necessarily true of nested types defined in dynamic assemblies or compiled with the Ilasm.exe (IL Assembler).

如需有關嵌套泛型型別,以及從其泛型型別定義來建立嵌套泛型型別的詳細資訊,請參閱 MakeGenericTypeFor information on nested generic types, and on constructing nested generic types from their generic type definitions, see MakeGenericType.

另請參閱

適用於