C# | Type.GetInterface() Method

Type.GetInterface() Method is used to gets a specific interface implemented or inherited by the current Type.

  • GetInterface(String) Method
  • GetInterface(String, Boolean) Method

GetInterface(String) Method

This method is used to search for the interface with the specified name.

Syntax: public Type GetInterface (string name);
Here, it takes the string containing the name of the interface to get. For generic interfaces, this is the mangled name.

Return Value: This method returns an object representing the interface with the specified name, implemented or inherited by the current Type, if found otherwise, null.



Exception: This method throws ArgumentNullException if the name is null .

Below programs illustrate the use of Type.GetInterface(String) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetInterface(String) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing object of Type
        Type objType = typeof(int);
  
        // try-catch block for handling Exception
        try {
  
            // Getting interface of specified name
            // using GetField(String) Method
            Type minterface = objType.GetInterface("IFormattable");
  
            // Display the Result
            Console.WriteLine("interface is: {0}", minterface);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.Write("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

interface is: System.IFormattable

Example 2: For ArgumentNullException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetInterface(String) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing object of Type
        Type objType = typeof(int);
  
        // try-catch block for handling Exception
        try {
  
            // Getting interface of specified name
            // using GetField(String) Method
            Type minterface = objType.GetInterface(null);
  
            // Display the Result
            Console.WriteLine("interface is: {0}", minterface);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.WriteLine("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

name is null.
Exception Thrown: System.ArgumentNullException

GetInterface(String, Boolean) Method

This method is used to search for the specified interface, specifying whether to do a case-insensitive search for the interface name when overridden in a derived class.

Syntax: public abstract Type GetInterface (string name, bool ignoreCase);

Parameters:

name: The string containing the name of the interface to get. For generic interfaces, this is the mangled name.



ignoreCase: true to ignore the case of that part of the name which specifies the simple interface name (the part that specifies the namespace must be correctly cased) Or false to perform a case-sensitive search for all parts of the name.

Return Value: This method returns n object representing the interface with the specified name, implemented or inherited by the current Type if found otherwise, null.

Exception: This method throws ArgumentNullException if name is null.

Below programs illustrate the use of the above-discussed method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetInterface(String, 
// Boolean) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing object of Type
        Type objType = typeof(int);
  
        // try-catch block for handling Exception
        try {
  
            // Getting interface of specified name
            // using GetField(String) Method
            Type minterface = objType.GetInterface("iformattable", true);
  
            // Display the Result
            Console.WriteLine("interface is: {0}", minterface);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) {
            Console.WriteLine("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

interface is: System.IFormattable

Example 2: For ArgumentNullException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetInterface(String, 
// Boolean) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing object of Type
        Type objType = typeof(int);
  
        // try-catch block for handling Exception
        try {
  
            // Getting interface of specified name
            // using GetField(String) Method
            Type minterface = objType.GetInterface(null, true);
  
            // Display the Result
            Console.WriteLine("interface is: {0}", minterface);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.WriteLine("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

name is null.
Exception Thrown: System.ArgumentNullException

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.