C# | Type.GetInterface() Method
Last Updated :
19 Jan, 2023
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:
Similar Reads
C# | Type.GetInterfaces() Method
Type.GetInterfaces() Method is used to get all the interfaces implemented or inherited by the current Type when overridden in a derived class. Syntax: public abstract Type[] GetInterfaces ();Return Value: This method returns an array of Type objects representing all the interfaces implemented or inh
2 min read
C# | Type.GetTypeCode() Method
Type.GetTypeCode() Method is used to get the underlying type code of the specified Type. Syntax: public static TypeCode GetTypeCode (Type type); Here, it takes the type whose underlying type code to get. Return Value: This method returns the code of the underlying type, or Empty if type is null. Bel
2 min read
C# | Type.GetTypeArray() Method
Type.GetTypeArray() Method is used to get the types of the objects in the specified array. Syntax: public static Type[] GetTypeArray (object[] args); Here, it takes an array of objects whose types to determine. Return Value: This method returns an array of Type objects representing the types of the
2 min read
C# | Type.GetTypeHandle() Method
Type.GetTypeHandle() Method is used to get the handle for the Type of a specified object. Syntax: public static RuntimeTypeHandle GetTypeHandle (object o); Here, it takes the object for which to get the type handle. Return Value: This method returns The handle for the Type of the specified Object. E
2 min read
C# | Type.GetNestedType() Method
Type.GetNestedType() Method is used to get a specific type nested within the current Type. There are 2 methods in the overload list of this method as follows: GetNestedType(String, BindingFlags) Method GetNestedType(String) Method GetNestedType(String, BindingFlags) Method This method is used to sea
4 min read
C# | Type.GetField() Method
Type.GetField() Method is used to get a specific field of the current Type. There are 2 methods in the overload list of this method as follows: GetField(String) Method GetField(String, BindingFlags) Method GetField(String) Method This method is used to search for the public field with the specified
4 min read
C# | Type.GetNestedTypes() Method
Type.GetNestedTypes() Method is used to get the types nested within the current Type. There are 2 methods in the overload list of this method as follows: GetNestedTypes() Method This method is used to return the public types nested in the current Type. Syntax: public Type[] GetNestedTypes ();Return
5 min read
C# | Type.GetFields() Method
Type.GetFields() Method is used to get the fields of the current Type. There are 2 methods in the overload list of this method as follows: GetFields() Method GetFields(BindingFlags) Method GetFields() Method This method is used to return all the public fields of the current Type. Syntax: public Syst
5 min read
C# | Type.GetMember() Method
Type.GetMember() Method is used to get the specified members of the current Type. There are 3 methods in the overload list of this method as follows: GetMember(String) Method GetMember(String, BindingFlags) Method GetMember(String, MemberTypes, BindingFlags) Method GetMember(String) Method This meth
6 min read
C# | Type.GetEnumName() Method
Type.GetEnumName(Object) Method is used to return the name of the constant which has the specified value for the current enumeration type. Syntax: public virtual string GetEnumName (object value); Here, it takes the value whose name is to be retrieved.Return Value: This method returns the name of th
3 min read