Type.GetNestedTypes Method ()
Returns all the types nested within the current Type.
[Visual Basic] Overloads Public Function GetNestedTypes() As Type() [C#] public Type[] GetNestedTypes(); [C++] public: Type* GetNestedTypes() []; [JScript] public function GetNestedTypes() : Type[];
Return Value
An array of Type objects representing all the types nested within the current Type.
-or-An empty array of type Type, if no types are nested within the current Type.
Remarks
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. 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. See note 2 below. |
- 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.
- For reflection, properties and events are hide-by-name-and-signature. 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.
- Custom attributes are not part of the common type system.
Example
[Visual Basic, C#, C++] The following example defines a nested class and a struct in MyClass and obtains objects of the nested types using the type object of MyClass.
[Visual Basic] Imports System Imports System.Reflection Public Class MyClass1 Public Class NestClass Public Shared myPublicInt As Integer = 0 End Class 'NestClass Public Structure NestStruct Public myPublicInt As Integer End Structure 'NestStruct End Class 'MyClass1 Public Class MyMainClass Public Shared Sub Main() Try ' Get the Type object corresponding to MyClass. Dim myType As Type = GetType(MyClass1) ' Get an array of nested type objects in MyClass. Dim nestType As Type() = myType.GetNestedTypes() Console.WriteLine("The number of nested types is {0}.", nestType.Length) Dim t As Type For Each t In nestType Console.WriteLine("Nested type is {0}.", t.ToString()) Next t Catch e As Exception Console.WriteLine("Error", e.Message.ToString()) End Try End Sub 'Main End Class 'MyMainClass [C#] using System; using System.Reflection; public class MyClass { public class NestClass { public static int myPublicInt=0; } public struct NestStruct { public static int myPublicInt=0; } } public class MyMainClass { public static void Main() { try { // Get the Type object corresponding to MyClass. Type myType=typeof(MyClass); // Get an array of nested type objects in MyClass. Type[] nestType=myType.GetNestedTypes(); Console.WriteLine("The number of nested types is {0}.", nestType.Length); foreach(Type t in nestType) Console.WriteLine("Nested type is {0}.", t.ToString()); } catch(Exception e) { Console.WriteLine("Error"+e.Message); } } } [C++] #using <mscorlib.dll> using namespace System; using namespace System::Reflection; public __gc class MyClass { public: __gc class NestClass { public: static int myPublicInt=0; }; __gc struct NestStruct { public: static int myPublicInt=0; }; }; int main() { try { // Get the Type object corresponding to MyClass. Type* myType=__typeof(MyClass); // Get an array of nested type objects in MyClass. Type* nestType[]=myType->GetNestedTypes(); Console::WriteLine(S"The number of nested types is {0}.",__box( nestType->Length)); System::Collections::IEnumerator* myEnum = nestType->GetEnumerator(); while (myEnum->MoveNext()) { Type* t = __try_cast<Type*>(myEnum->Current); Console::WriteLine(S"Nested type is {0}.", t); } } catch (Exception* e) { Console::WriteLine(S"Error {0}", e->Message); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard
See Also
Type Class | Type Members | System Namespace | Type.GetNestedTypes Overload List | GetNestedType