C# 中的 Type.GetTypeArray() 方法

csharpserver side programmingprogramming更新于 2025/9/24 5:37:17

C# 中的 Type.GetTypeArray() 方法用于获取指定数组中对象的类型。

语法

以下是语法 −

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

相关文章