Type.FindInterfaces(TypeFilter, Object) Metódus

Definíció

Egy objektumtömböt Type ad vissza, amely az aktuális Typeáltal implementált vagy örökölt interfészek szűrt listáját jelöli.

public:
 virtual cli::array <Type ^> ^ FindInterfaces(System::Reflection::TypeFilter ^ filter, System::Object ^ filterCriteria);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.Interfaces)]
public virtual Type[] FindInterfaces(System.Reflection.TypeFilter filter, object? filterCriteria);
public virtual Type[] FindInterfaces(System.Reflection.TypeFilter filter, object? filterCriteria);
public virtual Type[] FindInterfaces(System.Reflection.TypeFilter filter, object filterCriteria);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.Interfaces)>]
abstract member FindInterfaces : System.Reflection.TypeFilter * obj -> Type[]
override this.FindInterfaces : System.Reflection.TypeFilter * obj -> Type[]
abstract member FindInterfaces : System.Reflection.TypeFilter * obj -> Type[]
override this.FindInterfaces : System.Reflection.TypeFilter * obj -> Type[]
Public Overridable Function FindInterfaces (filter As TypeFilter, filterCriteria As Object) As Type()

Paraméterek

filter
TypeFilter

Az a meghatalmazott, amely összehasonlítja az illesztőket filterCriteriaa .

filterCriteria
Object

A keresési feltételek, amelyek meghatározzák, hogy a visszaadott tömb tartalmaz-e felületet.

Válaszok

Type[]

Az aktuálisan Typeimplementált vagy örökölt illesztők szűrt listáját képviselő objektumtömbType, vagy üres tömb, ha a szűrőnek megfelelő illesztők nincsenek implementálva vagy örökölve az aktuális Type.

Megvalósítás

Attribútumok

Kivételek

filter az null.

A rendszer meghív egy statikus inicializálót, és kivételt jelez.

Példák

Az alábbi példa megkeresi a megadott típus által implementált vagy örökölt felületet, majd megjeleníti a felület nevét.

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;
    }
}
open System
open System.Xml
open System.Reflection

let myInterfaceFilter (typeObj: Type) (criteriaObj: obj) =
    string typeObj = string criteriaObj

try
    let myXMLDoc = XmlDocument()
    myXMLDoc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" + "<title>Pride And Prejudice</title>" + "</book>")
    let myType = myXMLDoc.GetType()

    // Specify the TypeFilter delegate that compares the
    // interfaces against filter criteria.
    let myFilter = TypeFilter myInterfaceFilter
    let myInterfaceList =
        [ "System.Collections.IEnumerable"
          "System.Collections.ICollection" ]
    for i in myInterfaceList do
        let myInterfaces = myType.FindInterfaces(myFilter, i)
        if myInterfaces.Length > 0 then
            printfn $"\n{myType} implements the interface {i}."
            for j in myInterfaces do
                printfn $"Interfaces supported: {j}."
        else
            printfn $"\n{myType} does not implement the interface {i}."
with
| :? ArgumentNullException as e ->
    printfn $"ArgumentNullException: {e.Message}"
| :? TargetInvocationException as e ->
    printfn $"TargetInvocationException: {e.Message}"
| e ->
    printfn $"Exception: {e.Message}"

Imports System.Xml
Imports System.Reflection

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
    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

Megjegyzések

Ezt a metódust egy származtatott osztály felülírhatja.

Az Module.FilterTypeName osztály által Module.FilterTypeNameIgnoreCase biztosított és System.Reflection.Module meghatalmazottak is használhatók a meghatalmazott helyettSystem.Reflection.TypeFilter.

Az osztály által implementált összes interfészt a keresés során figyelembe vesszük, akár alaposztály, akár maga az osztály deklarálva van.

Ez a módszer megkeresi az alaposztály hierarchiáját, és visszaadja az egyes osztályok által implementálható egyező interfészeket, valamint az egyes illesztők mindegyikének megfelelő illesztőfelületeket (azaz az egyező felületek tranzitív bezárását). A rendszer nem ad vissza duplikált illesztőket.

Ha az aktuális Type érték egy típusparamétert jelöl egy általános típus vagy általános metódus definíciójában, FindInterfaces a típusparaméter korlátozásaiban deklarált összes illesztőben, valamint a kényszerekben deklarált interfészeken keresztül öröklő összes illesztőben keres. Ha az aktuális Type érték egy általános típusú típusargumentumot jelöl, FindInterfaces a típus által implementált összes illesztőt megkeresi, függetlenül attól, hogy azok megfelelnek-e a korlátozásoknak.

Note

FindInterfaces általános interfészeket is visszaadhat, még olyan típusok esetében is, amelyek nem általánosak. Egy nemgenerikus típus például IEnumerable<int> implementálhat (IEnumerable(Of Integer) Visual Basic).

A következőre érvényes:

Lásd még