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 inherited by the current Type or an empty array of type if no interfaces are implemented or inherited by the current Type.
Below programs illustrate the use of Type.GetInterfaces() Method:
Example 1:
C#
// C# program to demonstrate the // Type.GetInterfaces() 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 ); // Getting interface of specified name // using GetInterfaces(String) Method Type[] minterface = objType.GetInterfaces(); // Display the Result Console.WriteLine( "Interface present in type {0}" , objType); for ( int i = 0; i < minterface.Length; i++) Console.WriteLine( " {0}" , minterface[i]); } } |
Output:
Interface present in type System.Int32 System.IFormattable System.IComparable System.IComparable`1[System.Int32] System.IConvertible System.IEquatable`1[System.Int32]
Example 2: For if no public field is defined
C#
// C# program to demonstrate the // Type.GetInterfaces() 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 ( string ); // Getting interface of specified name // using GetInterfaces(String) Method Type[] minterface = objType.GetInterfaces(); // Display the Result Console.WriteLine( "Interface present in type {0}" , objType); for ( int i = 0; i < minterface.Length; i++) Console.WriteLine( " {0}" , minterface[i]); } } |
Output:
Interface present in type System.String System.ICloneable System.Collections.Generic.IEnumerable`1[System.Char] System.IComparable System.IComparable`1[System.String] System.IConvertible System.Collections.IEnumerable System.IEquatable`1[System.String]
Reference:
- https://docs.microsoft.com/en-us/dotnet/api/system.type.getinterfaces?view=netframework-4.8
Contact Us