Type.GetInterface Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

When overridden in a derived class, searches for the specified interface, specifying whether to do a case-insensitive search for the interface name.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax

Public MustOverride Function GetInterface ( _
    name As String, _
    ignoreCase As Boolean _
) As Type
public abstract Type GetInterface(
    string name,
    bool ignoreCase
)

Parameters

  • name
    Type: System..::.String
    The String containing the name of the interface to get. For generic interfaces, this is the mangled name.
  • ignoreCase
    Type: System..::.Boolean
    true to ignore the case of that part of name that 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 name.

Return Value

Type: System..::.Type
A Type object representing the interface with the specified name, implemented or inherited by the current Type, if found; otherwise, nullNothingnullptra null reference (Nothing in Visual Basic).

Exceptions

Exception Condition
ArgumentNullException

name is nullNothingnullptra null reference (Nothing in Visual Basic).

AmbiguousMatchException

The current Type represents a type that implements the same generic interface with different type arguments.

Remarks

The ignoreCase parameter applies only to the simple interface name, not to the namespace. The portion of name that specifies the namespace must have the correct case, or the interface will not be found. For example, the string "System.icomparable" finds the IComparable interface, but the string "system.icomparable" does not.

If the current Type represents a constructed generic type, this method returns the Type with the type parameters replaced by the appropriate type arguments.

If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the interface constraints and any interfaces inherited from class or interface constraints.

Note

For generic interfaces, the name parameter is the mangled name, ending with a grave accent () and the number of type parameters. This is true for both generic interface definitions and constructed generic interfaces. For example, to find IExample<T> (IExample(Of T) in Visual Basic) or IExample<string> (IExample(Of String) in Visual Basic), search for "IExample1".

Examples

The following example uses the GetInterface(String, Boolean) method overload to perform a case-sensitive search of the generic Dictionary<(Of <(TKey, TValue>)>) class for the generic IDictionary<(Of <(TKey, TValue>)>) interface.

The example also demonstrates the GetInterfaceMap method.

Imports System.Reflection
Imports System.Collections.Generic

Class Example

    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

        Dim obj As New Dictionary(Of Integer, String)

        If obj.GetType().GetInterface("IDictionary`2", False) IsNot Nothing Then

            ' Get the mapping between the interface methods of the 
            ' generic IDictionary interface and the Dictionary methods
            ' that implement them.
            Dim idictType As Type = GetType(IDictionary(Of Integer, String))
            Dim map As InterfaceMapping = _
                obj.GetType().GetInterfaceMap(idictType)

            Dim interfaceMethods() As MethodInfo = map.InterfaceMethods
            Dim implementationMethods() As MethodInfo = map.TargetMethods

            For i As Integer = 0 To interfaceMethods.Length - 1
                outputBlock.Text &= interfaceMethods(i).ToString() & _
                    " is implemented by:" & vbLf & "       " & _
                    implementationMethods(i).ToString() & vbLf
            Next
        End If
    End Sub 
End Class 

' This example produces the following output:
'
'System.String get_Item(Int32) is implemented by:
'       System.String get_Item(Int32)
'System.String set_Item(Int32, System.String) is implemented by:
'       Void set_Item(Int32, System.String)
'System.Collections.Generic.ICollection`1[System.Int32] get_Keys() is implemented by:
'       System.Collections.Generic.ICollection`1[System.Int32] System.Collections.Generic.IDictionary<TKey,TValue>.get_Keys()
'System.Collections.Generic.ICollection`1[System.String] get_Values() is implemented by:
'       System.Collections.Generic.ICollection`1[System.String] System.Collections.Generic.IDictionary<TKey,TValue>.get_Values()
'Boolean ContainsKey(Int32) is implemented by:
'       Boolean ContainsKey(Int32)
'Void Add(Int32, System.String) is implemented by:
'       Void Add(Int32, System.String)
'Boolean Remove(Int32) is implemented by:
'       Boolean Remove(Int32)
'Boolean TryGetValue(Int32, System.String ByRef) is implemented by:
'       Boolean TryGetValue(Int32, System.String ByRef)
using System;
using System.Reflection;
using System.Collections.Generic;

class Example
{

    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {

        Dictionary<int, string> obj = new Dictionary<int, string>();

        // Get the mapping between the interface methods of the 
        // generic IDictionary interface and the Dictionary methods
        // that implement them.
        if (obj.GetType().GetInterface("IDictionary`2", false) != null)
        {
            Type idictType = typeof(IDictionary<int, string>);
            InterfaceMapping map = obj.GetType().GetInterfaceMap(idictType);

            MethodInfo[] interfaceMethods = map.InterfaceMethods;
            MethodInfo[] implementationMethods = map.TargetMethods;

            for(int i=0; i<interfaceMethods.Length; i++)
            {
                outputBlock.Text += interfaceMethods[i].ToString() + 
                    " is implemented by:\n       " + 
                    implementationMethods[i].ToString() + "\n";
            }
        }
    }
}

/* This example produces the following output:

System.String get_Item(Int32) is implemented by:
       System.String get_Item(Int32)
System.String set_Item(Int32, System.String) is implemented by:
       Void set_Item(Int32, System.String)
System.Collections.Generic.ICollection`1[System.Int32] get_Keys() is implemented by:
       System.Collections.Generic.ICollection`1[System.Int32] System.Collections.Generic.IDictionary<TKey,TValue>.get_Keys()
System.Collections.Generic.ICollection`1[System.String] get_Values() is implemented by:
       System.Collections.Generic.ICollection`1[System.String] System.Collections.Generic.IDictionary<TKey,TValue>.get_Values()
Boolean ContainsKey(Int32) is implemented by:
       Boolean ContainsKey(Int32)
Void Add(Int32, System.String) is implemented by:
       Void Add(Int32, System.String)
Boolean Remove(Int32) is implemented by:
       Boolean Remove(Int32)
Boolean TryGetValue(Int32, System.String ByRef) is implemented by:
       Boolean TryGetValue(Int32, System.String ByRef)
 */

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

Type Class

System Namespace

String

GetInterfaces