Open In App

C# | Type.GetInterface() Method

Last Updated : 19 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

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: 

csharp




// 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 initializing object of Type
        Type objType = typeof(int);
 
        // try-catch block for handling Exception
        try {
 
            // Getting interface of specified name
            // using GetInterface(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);
        }
    }
}


Output:

interface is: System.IFormattable

Example 2: For ArgumentNullException 

csharp




// 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 initializing object of Type
        Type objType = typeof(int);
 
        // try-catch block for handling Exception
        try {
 
            // Getting interface of specified name
            // using GetInterface(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);
        }
    }
}


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: 

csharp




// 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 initializing object of Type
        Type objType = typeof(int);
 
        // try-catch block for handling Exception
        try {
 
            // Getting interface of specified name
            // using GetInterface(String,Boolean) 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);
        }
    }
}


Output:

interface is: System.IFormattable

Example 2: For ArgumentNullException 

csharp




// 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 initializing object of Type
        Type objType = typeof(int);
 
        // try-catch block for handling Exception
        try {
 
            // Getting interface of specified name
            // using GetInterface(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);
        }
    }
}


Output:

name is null.
Exception Thrown: System.ArgumentNullException

Reference:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads