เมธอด Type.GetNestedTypes() ใน C# ใช้สำหรับดึงประเภทที่ซ้อนอยู่ภายใน Type ปัจจุบัน
ไวยากรณ์
เมธอดนี้มี 2 การโหลดทับ:
public Type[] GetNestedTypes (); public abstract Type[] GetNestedTypes (System.Reflection.BindingFlags bindingAttr);
ตัวอย่าง
ตัวอย่างการใช้งานเมธอด Type.GetNestedTypes():
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
ตัวอย่างเพิ่มเติม
ตัวอย่างที่สองกับ 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