C# | Type.GetInterface() Method - GeeksforGeeks

05 Dec, 2019
Type.GetInterface() Method is used to gets a specific interface implemented or inherited by the current Type.
GetInterface(String) Method
GetInterface(String, Boolean) 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:
// C# program to demonstrate the// Type.GetInterface(String) Methodusing 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 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);        }    }}
interface is: System.IFormattable

Example 2: For ArgumentNullException
// C# program to demonstrate the// Type.GetInterface(String) Methodusing 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 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);        }    }}
name is null.
Exception Thrown: System.ArgumentNullException

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:
// C# program to demonstrate the// Type.GetInterface(String, // Boolean) Methodusing 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 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);        }    }}
interface is: System.IFormattable

Example 2: For ArgumentNullException
// C# program to demonstrate the// Type.GetInterface(String, // Boolean) Methodusing 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 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);        }    }}
Output:
name is null.
Exception Thrown: System.ArgumentNullException

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.type.getinterface?view=netframework-4.8
CSharp-method
CSharp-Type-Class
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C# Dictionary with examples
C# | Method Overriding
C# | String.IndexOf( ) Method | Set - 1
Extension Method in C#
C# | Delegates
Introduction to .NET Framework
Difference between Ref and Out keywords in C#
HashSet in C# with Examples
C# | Replace() Method
C# | Abstract Classes