The Type.GetNestedTypes() method in C# is used to get the types nested within the current Type.
Syntax
Following is the syntax −
public Type[] GetNestedTypes (); public abstract Type[] GetNestedTypes (System.Reflection.BindingFlags bindingAttr);
Example
Let us now see an example to implement the Type.GetNestedTypes() method −
using System;
public class Demo {
public static void Main(){
Type type1 = typeof(Subject);
try {
Type[] type2 = type1.GetNestedTypes();
Console.WriteLine("Nested Types...");
for (int i = 0; i < type2.Length; i++)
Console.WriteLine("{0} ", type2[i]);
}
catch (ArgumentNullException e){
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
public class Subject{
public class BasicSubject {
//
}
public class AdvSubject {
//
}
}Output
This will produce the following output −
Nested Types... Subject+BasicSubject Subject+AdvSubject
Example
Let us now see another example to implement the Type.GetNestedTypes() method −
using System;
using System.Reflection;
public class Demo {
public static void Main(){
Type type1 = typeof(Subject);
try {
Type[] type2 = type1.GetNestedTypes(BindingFlags.Public);
Console.WriteLine("Nested Types...");
for (int i = 0; i < type2.Length; i++)
Console.WriteLine("{0} ", type2[i]);
}
catch (ArgumentNullException e){
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
public class Subject{
public class BasicSubject {
//
}
public class AdvSubject {
//
}
}Output
This will produce the following output −
Nested Types... Subject+BasicSubject Subject+AdvSubject