Type.GetTypeArray()方法用於獲取指定數組中對象的類型。
用法: public static Type[] GetTypeArray (object[] args);
Here, it takes an array of objects whose types to determine.
返回值:此方法返回一個Type對象數組,該數組代表args中相應元素的類型。
異常:如果args為null或args中的一個或多個元素為null,則此方法將引發ArgumentNullException。
以下示例程序旨在說明Type.GetTypeArray()方法的使用:
示例1:
// C# program to demonstrate the
// Type.GetTypeArray(Object[]) Method
using System;
using System.Globalization;
using System.Reflection;
// Defining class Empty
public class Empty { }
class GFG {
// Main Method
public static void Main()
{
// try-catch block for handling Exception
try {
// creating and initializing object
object[] obj = {2, 3.4, 'c', "ram"};
// using GetProperties() Method
Type[] type = Type.GetTypeArray(obj);
// Display the Result
Console.WriteLine("Types of the objects in the specified array: ");
for (int i = 0; i < type.Length; i++)
Console.WriteLine(" {0}", type[i]);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.Write("One or more of the elements in args is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
Types of the objects in the specified array: System.Int32 System.Double System.Char System.String
示例2:
// C# program to demonstrate the
// Type.GetTypeArray(Object[]) Method
using System;
using System.Globalization;
using System.Reflection;
// Defining class Empty
public class Empty {}
class GFG {
// Main Method
public static void Main()
{
// try-catch block for handling Exception
try {
// creating and initializing object
object[] obj = {2, 3.4, 'c', "ram", null};
// using GetProperties() Method
Type[] type = Type.GetTypeArray(obj);
// Display the Result
Console.WriteLine("Types of the objects in the specified array: ");
for (int i = 0; i < type.Length; i++)
Console.WriteLine(" {0}", type[i]);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.WriteLine("One or more of the elements in args is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
One or more of the elements in args is null. Exception Thrown: System.ArgumentNullException
參考:
相關用法
- C# DateTimeOffset.Add()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Dictionary.Add()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Type.GetTypeArray() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。