Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức Type.GetTypeArray() trong C#

Phương thức Type.GetTypeArray() trong C# được dùng để lấy các kiểu của các đối tượng trong mảng được chỉ định.

Cú pháp

Cú pháp như sau:

public static Type[] GetTypeArray(object[] args);

Tham số args là mảng các đối tượng cần xác định kiểu.

Ví dụ 1

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]);
    }
}

Kết quả

Types...
System.Double
System.Int32
System.Int32
System.Int32
System.String

Ví dụ 2

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]);
    }
}

Kết quả

Types...
System.Decimal
System.Double
System.Decimal
System.Int32
System.String