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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。