Share via


Type.FindInterfaces メソッド

現在の Type によって実装または継承されているインターフェイスのフィルタ適用済みリストを表す、 Type オブジェクトの配列を返します。

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

パラメータ

  • filter
    インターフェイスを filterCriteria と比較する TypeFilter デリゲート。
  • filterCriteria
    返される配列に、検出したインターフェイスを含めるかどうかを判断する検索条件。

戻り値

現在の Type によって実装または継承されているインターフェイスのフィルタ適用済みリストを表す、 Type オブジェクトの配列。

または

フィルタ条件に一致するインターフェイスが現在の Type で実装または継承されていない場合は、 Type 型の空の配列。

例外

例外の種類 条件
ArgumentNullException filter が null 参照 (Visual Basic では Nothing) です。
TargetInvocationException 静的初期化子が呼び出され、例外をスローします。

解説

このメソッドは、派生クラスでオーバーライドできます。

System.Reflection.TypeFilter デリゲートの代わりに、 System.Reflection.Module クラスによって提供された Module.FilterTypeName デリゲートおよび Module.FilterTypeNameIgnoreCase デリゲートを使用することもできます。

このクラスによって実装されるすべてのインターフェイスは、基本クラスまたはこのクラス自体のどちらで宣言されている場合でも、検索時に検索対象として認識されます。

このメソッドは、基本クラス階層を検索し、各クラスが実装していて検索条件に一致した各インターフェイスと、それらの各インターフェイスが実装していて検索条件に一致したインターフェイスをすべて返します (つまり、検索条件に一致する、中間の階層にあるインターフェイスも返します)。重複するインターフェイスは返されません。

使用例

[Visual Basic, C#, C++] 指定した型によって実装または継承される指定したインターフェイスを検索し、そのインターフェイス名を表示する例を次に示します。

 

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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

Type クラス | Type メンバ | System 名前空間 | Module | TypeFilter | GetInterface | GetInterfaces