TypeFilter 委托

筛选在 Type 对象数组中表示的类。

**命名空间:**System.Reflection
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Delegate Function TypeFilter ( _
    m As Type, _
    filterCriteria As Object _
) As Boolean
用法
Dim instance As New TypeFilter(AddressOf HandlerMethod)
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public delegate bool TypeFilter (
    Type m,
    Object filterCriteria
)
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public delegate bool TypeFilter (
    Type^ m, 
    Object^ filterCriteria
)
/** @delegate */
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public delegate boolean TypeFilter (
    Type m, 
    Object filterCriteria
)
JScript 支持使用委托,但不支持进行新的声明。

参数

  • m
    筛选器应用到的 Type 对象。
  • filterCriteria
    用于筛选列表的任意对象。

返回值

如果包括已筛选列表中的 Type,则为 true;否则为 false

备注

TypeFilter 委托用于筛选类列表。具体说来就是使用该委托筛选在 Type 对象数组中表示的类。Type.FindInterfaces 方法使用该委托筛选它返回的接口列表。DelegateMulticastDelegate 的每个派生类都具有一个构造函数和一个 DynamicInvoke 方法。请参见 Delegate 说明中给出的 C++ 托管扩展代码示例。

示例

此示例显示如何定义与 TypeFilter 委托原型匹配的方法,该方法允许使用反射筛选或返回匹配项的子集。

Imports system
Imports system.Reflection

' This interface is defined in this assembly.
Public Interface IBookRetailer
    Sub Purchase()
    Sub ApplyDiscount()
End Interface

' This interface is also defined in this assembly.
Public Interface IMusicRetailer
    Sub Purchase()
End Interface

' This class implements four interfaces; two are defined in this assembly and two are defined in another assembly.
Public Class MyRetailer
    Implements IBookRetailer, IMusicRetailer, IComparable, ICloneable

    ' For demonstration purposes, this method returns nothing; it just allows the code to compile.
    Public Function Clone() As Object Implements System.ICloneable.Clone
        Return Nothing
    End Function

    ' For demonstration purposes, this method returns nothing; it just allows the code to compile.
    Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
        Return Nothing
    End Function

    ' For demonstration purposes, this method returns nothing; it just allows the code to compile.
    Public Sub ApplyDiscout() Implements IBookRetailer.ApplyDiscount
    End Sub

    ' For demonstration purposes, this method returns nothing; it just allows the code to compile.
    Public Sub Purchase() Implements IBookRetailer.Purchase
    End Sub

    ' For demonstration purposes, this method returns nothing; it just allows the code to compile.
    Public Sub Purchase1() Implements IMusicRetailer.Purchase
    End Sub
End Class

Module Module1

    Sub Main()
        ' Find the interfaces defined by the MyRetailer class. Each interface found is passed to
        ' the TypeFilter method which checks if the interface is defined in the executing assembly.
        Dim interfaces() As Type = _
            GetType(MyRetailer).FindInterfaces(AddressOf TypeFilter, Assembly.GetExecutingAssembly().GetName().ToString())

        ' Show the interfaces that are defined in this assembly that are also implemented by MyRetailer.
        Console.WriteLine("MyRetailer implements the following interfaces (defined in this assembly):")
        Dim t As Type
        For Each t In interfaces
            Console.WriteLine("   {0}", t.Name)
        Next
    End Sub

    ' This method is called by the FindInterfaces method. 
    ' This method is called once per interface defined.
    Function TypeFilter(ByVal t As Type, ByVal filterCriteria As Object) As Boolean
        ' Return true if interface is defined in the same 
        ' assembly identified by the filterCriteria object.
        Return t.Assembly.GetName().ToString() = CType(filterCriteria, String)
    End Function

End Module

' Output will vary based on the contents of the C drive.
'
' This code produces the following output.
' MyRetailer implements the following interfaces (defined in this assembly):
'   IBookRetailer
'   IMusicRetailer

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

System.Reflection 命名空间
FindInterfaces
Object
Delegate
MulticastDelegate