Expand Minimize

Type.FindInterfaces Method

Returns an array of Type objects representing a filtered list of interfaces implemented or inherited by the current Type.

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

'Declaration
Public Overridable Function FindInterfaces ( _
	filter As TypeFilter, _
	filterCriteria As Object _
) As Type()

Parameters

filter
Type: System.Reflection.TypeFilter

The delegate that compares the interfaces against filterCriteria.

filterCriteria
Type: System.Object

The search criteria that determines whether an interface should be included in the returned array.

Return Value

Type: System.Type()
An array of Type objects representing a filtered list of the interfaces implemented or inherited by the current Type, or an empty array of type Type if no interfaces matching the filter are implemented or inherited by the current Type.

Implements

_Type.FindInterfaces(TypeFilter, Object)

ExceptionCondition
ArgumentNullException

filter is Nothing.

TargetInvocationException

A static initializer is invoked and throws an exception.

This method can be overridden by a derived class.

The Module.FilterTypeName and Module.FilterTypeNameIgnoreCase delegates supplied by the System.Reflection.Module class may also be used, in lieu of the System.Reflection.TypeFilter delegate.

All of the interfaces implemented by this class are considered during the search, whether declared by a base class or this class itself.

This method searches the base class hierarchy, returning each of the matching interfaces each class implements as well as all the matching interfaces each of those interfaces implements (that is, the transitive closure of the matching interfaces is returned). No duplicate interfaces are returned.

If the current Type represents a type parameter in the definition of a generic type or generic method, FindInterfaces searches all the interfaces declared in the constraints on the type parameter, and all interfaces inherited through the interfaces declared in the constraints. If the current Type represents a type argument of a generic type, FindInterfaces searches all the interfaces implemented by the type, whether or not they match constraints.

NoteNote

FindInterfaces can return generic interfaces, even on types that are not generic. For example, a nongeneric type might implement IEnumerable<int> (IEnumerable(Of Integer) in Visual Basic).

The following example finds the specified interface implemented or inherited by the specified type, and then displays the interface names.


Imports System
Imports System.Xml
Imports System.Reflection
Imports Microsoft.VisualBasic

Public Class MyFindInterfacesSample
    Public Shared Sub Main()
        Try 
            Dim myXMLDoc As New XmlDocument()
            myXMLDoc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" _
                & "<title>Pride And Prejudice</title>" & "</book>")
            Dim myType As Type = myXMLDoc.GetType()

            ' Specify the TypeFilter delegate that compares the  
            ' interfaces against filter criteria. 
            Dim myFilter As New TypeFilter(AddressOf MyInterfaceFilter)
            Dim myInterfaceList() As String = _
                {"System.Collections.IEnumerable", _
                "System.Collections.ICollection"}
            Dim index As Integer 
            For index = 0 To myInterfaceList.Length - 1
                Dim myInterfaces As Type() = _
                    myType.FindInterfaces(myFilter, myInterfaceList(index))
                If myInterfaces.Length > 0 Then
                    Console.WriteLine(ControlChars.Cr & _
                        "{0} implements the interface {1}.", _
                        myType, myInterfaceList(index))
                    Dim j As Integer 
                    For j = 0 To myInterfaces.Length - 1
                        Console.WriteLine("Interfaces supported: {0}", _
                            myInterfaces(j).ToString())
                    Next j
                Else
                    Console.WriteLine(ControlChars.Cr & _
                        "{0} does not implement the interface {1}.", _
                        myType, myInterfaceList(index))
                End If 
            Next index
        Catch e As ArgumentNullException
            Console.WriteLine("ArgumentNullException: " & e.Message)
        Catch e As TargetInvocationException
            Console.WriteLine("TargetInvocationException: " & e.Message)
        Catch e As Exception
            Console.WriteLine("Exception: " & e.Message)
        End Try 
    End Sub 'Main
    Public Shared Function MyInterfaceFilter(ByVal typeObj As Type, _
        ByVal criteriaObj As [Object]) As Boolean 
        If typeObj.ToString() = criteriaObj.ToString() Then 
            Return True 
        Else 
            Return False 
        End If 
    End Function 'MyInterfaceFilter 
End Class 'MyFindInterfacesSample

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft