Type.GetTypeArray() วิธีการใน C# ใช้เพื่อรับประเภทของวัตถุในอาร์เรย์ที่ระบุ
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
public static Type[] GetTypeArray (object[] args);
อาร์กิวเมนต์ arg คืออาร์เรย์ของอ็อบเจ็กต์ที่ต้องระบุประเภท
ตัวอย่าง
ให้เราดูตัวอย่างการใช้งานเมธอด Type.GetTypeArray() -
using System;
using System.Reflection;
public class Demo {
public static void Main(){
Object[] obj = new Object[5];
obj[0] = 10.20;
obj[1] = 20;
obj[2] = 30;
obj[3] = 40;
obj[4] = "tom";
Type[] type = Type.GetTypeArray(obj);
Console.WriteLine("Types...");
for (int i = 0; i < type.Length; i++)
Console.WriteLine(" {0}", type[i]);
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Types... System.Double System.Int32 System.Int32 System.Int32 System.String
ตัวอย่าง
ให้เราดูตัวอย่างอื่นเพื่อนำวิธีการ Type.GetTypeArray() ไปใช้ -
using System;
using System.Reflection;
public class Demo {
public static void Main(){
Object[] obj = new Object[5];
obj[0] = 100m;
obj[1] = 20.98;
obj[2] = 30.788m;
obj[3] = 40;
obj[4] = "jack";
Type[] type = Type.GetTypeArray(obj);
Console.WriteLine("Types...");
for (int i = 0; i < type.Length; i++)
Console.WriteLine(" {0}", type[i]);
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Types... System.Decimal System.Double System.Decimal System.Int32 System.String