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