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);
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- 要傳回 nullDefaultDefault to return null.

傳回

Type

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

實作

例外狀況

namenullname is null.

備註

使用的嵌套類別的簡單名稱 nameUse 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. 例如,使用「內部1」字串 ` 來取得 Inner<T> Visual Basic) 中的泛型嵌套類別 (Inner(Of T)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 代表在 c #、Visual Basic 或 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. 這不一定適用于動態元件中定義的巢狀型別,或使用 Ilasm.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);
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.

傳回

Type

物件,代表具有指定名稱的巢狀類型 (如有找到),否則為 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.

使用的嵌套類別的簡單名稱 nameUse 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. 例如,使用「內部1」字串 ` 來取得 Inner<T> Visual Basic) 中的泛型嵌套類別 (Inner(Of T)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.

下表顯示在反映型別時,方法會傳回基類的成員 GetThe 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 不適用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. 依名稱和簽章會考慮簽章的所有部分,包括自訂修飾詞、傳回類型、參數類型、個 sentinel 和非受控呼叫慣例。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 代表在 c #、Visual Basic 或 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. 這不一定適用于動態元件中定義的巢狀型別,或使用 Ilasm.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.

另請參閱

適用於