Type.GetTypeHandle()方法用於獲取指定對象的Type的句柄。
用法: public static RuntimeTypeHandle GetTypeHandle (object o);
Here, it takes the object for which to get the type handle.
返回值:此方法返回指定對象的Type的句柄。
異常:如果o為null,則此方法引發ArgumentNullException。
以下示例程序旨在說明Type.GetTypeHandle()方法的使用:
示例1:
// C# program to demonstrate the
// Type.GetTypeHandle() Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// try-catch block for handling Exception
try {
// creating and initializing object Type
Type type = typeof(int);
// getting handle of given type
// by using GetTypeHandle() Method
RuntimeTypeHandle handle = Type.GetTypeHandle(type);
// Display the Result
Console.WriteLine("Type referenced is {0}", handle);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.WriteLine("object is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
Type referenced is System.RuntimeTypeHandle
示例2:
// C# program to demonstrate the
// Type.GetTypeHandle() Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// try-catch block for handling Exception
try {
// creating and initializing object Type
Type type = typeof(int);
// getting handle of given type
// by using GetTypeHandle() Method
RuntimeTypeHandle handle = Type.GetTypeHandle(null);
// Display the Result
Console.WriteLine("Type referenced is {0}", handle);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.WriteLine("object is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
object 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.GetTypeHandle() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。