C# | Type.GetTypeFromHandle() Method

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Type referenced is System.RuntimeType
Attributes are: AutoLayout, AnsiClass, Class, SequentialLayout, Serializable, BeforeFieldInit

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Type referenced is System.RuntimeType
Attributes are: AutoLayout, AnsiClass, Class, SequentialLayout, Serializable, BeforeFieldInit

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.