ภาพรวม
เมธอด Type.GetNestedTypes() คืนอาร์เรย์ Type[] ของทุกชนิดข้อมูลที่ประกาศซ้อน (nested) ภายในชนิดนั้น สามารถระบุ BindingFlags ได้
ตัวอย่างที่ 1: ไม่ระบุ BindingFlags (default: public instance)
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 { }
}
ผลลัพธ์
Nested Types... Subject+BasicSubject Subject+AdvSubject
ตัวอย่างที่ 2: ระบุ BindingFlags.Public
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 { }
}
ผลลัพธ์
Nested Types... Subject+BasicSubject Subject+AdvSubject
สรุป
- Nested types แสดงชื่อเป็น
OuterType+NestedType - ใช้
BindingFlagsควบคุม public/non-public, static/instance - หากไม่มี nested type คืนอาร์เรย์ว่าง