Type.GetTypeFromHandle() Method is used to get the type referenced by the specified type handle.
Syntax: public static Type GetTypeFromHandle (RuntimeTypeHandle handle);
Here, it takes the object which refers to the type.
Return Value: This method returns the type referenced by the specified RuntimeTypeHandle, or null if the Value property of handle is null.
Below programs illustrate the use of Type.GetTypeFromHandle() Method:
Example 1:
using System;
using System.Globalization;
using System.Reflection;
class GFG {
public static void Main()
{
Type type = typeof ( int );
RuntimeTypeHandle handle = Type.GetTypeHandle(type);
Type outtype = Type.GetTypeFromHandle(handle);
Console.WriteLine( "Type referenced is {0}" , outtype);
Console.WriteLine( "Attributes are: " + outtype.Attributes);
}
}
|
Output:
Type referenced is System.RuntimeType
Attributes are: AutoLayout, AnsiClass, Class, SequentialLayout, Serializable, BeforeFieldInit
Example 2:
using System;
using System.Globalization;
using System.Reflection;
struct Empty {}
class GFG {
public static void Main()
{
Type type = typeof (Empty);
RuntimeTypeHandle handle = Type.GetTypeHandle(type);
Type outtype = Type.GetTypeFromHandle(handle);
Console.WriteLine( "Type referenced is {0}" , outtype);
Console.WriteLine( "Attributes are: " + outtype.Attributes);
}
}
|
Output:
Type referenced is System.RuntimeType
Attributes are: AutoLayout, AnsiClass, Class, SequentialLayout, Serializable, BeforeFieldInit
Reference: