Export (0) Print
Expand All

Type.FindInterfaces Method

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

[Visual Basic]
Public Overridable Function FindInterfaces( _
   ByVal filter As TypeFilter, _
   ByVal filterCriteria As Object _
) As Type()
[C#]
public virtual Type[] FindInterfaces(
 TypeFilter filter,
 object filterCriteria
);
[C++]
public: virtual Type* FindInterfaces(
 TypeFilter* filter,
 Object* filterCriteria
) [];
[JScript]
public function FindInterfaces(
   filter : TypeFilter,
 filterCriteria : Object
) : Type[];

Parameters

filter
The TypeFilter delegate that compares the interfaces against filterCriteria.
filterCriteria
The search criteria that determines whether an interface should be included in the returned array.

Return Value

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.

Exceptions

Exception Type Condition
ArgumentNullException filter is a null reference (Nothing in Visual Basic).
TargetInvocationException A static initializer is invoked and throws an exception.

Remarks

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.

Example

[Visual Basic, C#, C++] The following example finds the specified interface implemented or inherited by the specified type, and then displays the interface names.

[Visual Basic] 

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

[C#] 
using System;
using System.Xml;
using System.Reflection;

public class MyFindInterfacesSample 
{
    public static void Main()
    {
        try
        {
            XmlDocument myXMLDoc = new XmlDocument();
            myXMLDoc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" + "</book>");
            Type myType = myXMLDoc.GetType();

            // Specify the TypeFilter delegate that compares the interfaces against filter criteria.
            TypeFilter myFilter = new TypeFilter(MyInterfaceFilter);
            String[] myInterfaceList = new String[2] {"System.Collections.IEnumerable",
                                                         "System.Collections.ICollection"};
            for(int index=0; index < myInterfaceList.Length; index++)
            {
                Type[] myInterfaces = myType.FindInterfaces(myFilter, myInterfaceList[index]);
                if (myInterfaces.Length > 0) 
                {
                    Console.WriteLine("\n{0} implements the interface {1}.", myType, myInterfaceList[index]);    
                    for(int j =0;j < myInterfaces.Length;j++)
                        Console.WriteLine("Interfaces supported: {0}.", myInterfaces[j].ToString());
                }
                else
                    Console.WriteLine("\n{0} does not implement the interface {1}.",myType,myInterfaceList[index]);    
            }
        }
        catch(ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException: " + e.Message);
        }
        catch(TargetInvocationException e)
        {
            Console.WriteLine("TargetInvocationException: " + e.Message);
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
    }
      
    public static bool MyInterfaceFilter(Type typeObj,Object criteriaObj)
    {
        if(typeObj.ToString() == criteriaObj.ToString())
            return true;
        else
            return false;
    }
}

[C++] 
#using <mscorlib.dll>
#using <system.xml.dll>

using namespace System;
using namespace System::Xml;
using namespace System::Reflection;

public __gc class MyFindInterfacesSample {

public:
   static bool MyInterfaceFilter(Type* typeObj, Object* criteriaObj) {
      if (typeObj->ToString()->Equals(criteriaObj->ToString()))
         return true;
      else
         return false;
   }
};
int main() {
   try {
      XmlDocument* myXMLDoc = new XmlDocument();
      myXMLDoc->LoadXml(S"<book genre='novel' ISBN='1-861001-57-5'> <title>Pride And Prejudice</title> </book>");
      Type*  myType = myXMLDoc->GetType();

      // Specify the TypeFilter delegate that compares the interfaces against filter criteria.
      TypeFilter* myFilter = new TypeFilter(0, MyFindInterfacesSample::MyInterfaceFilter);
      String* myInterfaceList[] = {S"System.Collections.IEnumerable",
         S"System.Collections.ICollection"};
      for (int index=0; index < myInterfaceList->Length; index++) {
         Type*  myInterfaces[] = myType->FindInterfaces(myFilter, myInterfaceList[index]);
         if (myInterfaces->Length > 0) {
            Console::WriteLine(S"\n{0} implements the interface {1}.", myType, myInterfaceList[index]);    
            for (int j =0;j < myInterfaces->Length;j++)
               Console::WriteLine(S"Interfaces supported: {0}.", myInterfaces[j]);
         } else
            Console::WriteLine(S"\n{0} does not implement the interface {1}.", myType, myInterfaceList[index]);    
      }
   } catch (ArgumentNullException* e) {
      Console::WriteLine(S"ArgumentNullException: {0}", e->Message);
   } catch (TargetInvocationException* e) {
      Console::WriteLine(S"TargetInvocationException: {0}", e->Message);
   } catch (Exception* e) {
      Console::WriteLine(S"Exception: {0}", e->Message);
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Type Class | Type Members | System Namespace | Module | TypeFilter | GetInterface | GetInterfaces

Show:
© 2014 Microsoft