เมธอด Type.GetTypeArray() ใน C# ใช้สำหรับดึงประเภทของออบเจ็กต์ในอาร์เรย์ที่ระบุ
ไวยากรณ์
ไวยากรณ์มีดังนี้:
public static Type[] GetTypeArray (object[] args);
พารามิเตอร์ args คืออาร์เรย์ของออบเจ็กต์ที่ต้องการกำหนดประเภท
ตัวอย่าง
ตัวอย่างการใช้งานเมธอด 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
ตัวอย่างเพิ่มเติม
ตัวอย่างที่สองกับประเภทตัวเลขต่างๆ:
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