Export (0) Print
Expand All
Expand Minimize

TypeFilter Delegate

Filters the classes represented in an array of Type objects.

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

'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Delegate Function TypeFilter ( _
	m As Type, _
	filterCriteria As Object _
) As Boolean
'Usage
Dim instance As New TypeFilter(AddressOf HandlerMethod)
/** @delegate */
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public delegate boolean TypeFilter (
	Type m, 
	Object filterCriteria
)
JScript supports the use of delegates, but not the declaration of new ones.

Parameters

m

The Type object to which the filter is applied.

filterCriteria

An arbitrary object used to filter the list.

Return Value

true to include the Type in the filtered list; otherwise false.

The TypeFilter delegate is used to filter a list of classes. Specifically, you use it to filter the classes represented in an array of Type objects. The Type.FindInterfaces method uses this delegate to filter the list of interfaces that it returns. Every derived class of Delegate and MulticastDelegate has a constructor and a DynamicInvoke method. See the Managed Extensions for C++ code example given in the description for Delegate.

This example shows how to define a method matching the TypeFilter delegate prototype allowing you to use reflection to filter or return a subset of matching entries.

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

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft