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:
// C# program to demonstrate the // Type.GetTypeFromHandle() Method using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// creating and initializing object Type
Type type = typeof ( int );
// creating object of RuntimeTypeHandle
// and getting instance of type
RuntimeTypeHandle handle = Type.GetTypeHandle(type);
// Getting the type referenced by
// the specified RuntimeTypeHandle,
// using GetTypeFromHandle() Method
Type outtype = Type.GetTypeFromHandle(handle);
// Display the Result
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:
// C# program to demonstrate the // Type.GetTypeFromHandle() Method using System;
using System.Globalization;
using System.Reflection;
// Defining Empty struct struct Empty {}
class GFG {
// Main Method
public static void Main()
{
// creating and initializing object Type
Type type = typeof (Empty);
// creating object of RuntimeTypeHandle
// and getting instance of type
RuntimeTypeHandle handle = Type.GetTypeHandle(type);
// Getting the type referenced by
// the specified RuntimeTypeHandle,
// using GetTypeFromHandle() Method
Type outtype = Type.GetTypeFromHandle(handle);
// Display the Result
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: