Recipe 9.7. An Advanced Interface Search MechanismProblemYou are searching for an interface using the Type class. However, complex interface searches are not available through the GetInterface and GetInterfaces methods of a Type object. The GetInterface method searches for an interface only by name (using a case-sensitive or case-insensitive search), and the GetInterfaces method returns an array of all the interfaces implemented on a particular type. You want a more focused searching mechanism that might involve searching for interfaces that define a method with a specific signature or implemented interfaces that are loaded from the GAC. You need more flexible and more advanced searching for interfaces that does not involve creating your own interface search engine. This capability might be used for applications like a code generator or reverse engineering tool. SolutionThe FindInterfaces method of a Type object can be used along with a callback to perform complex searches of interfaces on a type. The method shown in Example 9-8 will call a custom interface searching method, SearchInterfacesOfType. Example 9-8. Performing complex searches of interfaces on a type
The FindSpecificInterfaces method searches for any of the three interface types contained in the Names array that are implemented by the System.Collections.ArrayList type. The SearchInterfacesOfType method accepts a type (searchedType) on which to search for interfaces and an array of types (interfaceNames) that contains criteria for the search. For this method, the criterion is a Type array of interfaces. This method then calls the FindInterfaces method on the searchedType parameter, passing in a delegate and the Type array criteria of interfaces. (The delegate will be invoked for each interface found.) This method then returns an array of interface types that match the criterion. The TypeFilter delegate, filter, defines the IfaceFilterCallback method to be called for each interface found on the searchedType object. The real power of this search mechanism lies in the IfaceFilterCallback callback method. This callback searches for each of the interface types in the criteria array that is implemented by the searchedType parameter of the SearchInterfacesOfType method. DiscussionThe FindInterfaces method of a Type object makes use of the TypeFilter delegate, which is passed to the filter parameter. This delegate is supplied by the FCL and allows an extra layer of filtering (of any type that you want) to occur. This delegate returns a Boolean value, where true indicates that the ifaceType object passed to this delegate should be included in the Type array that the FindInterfaces method returns; false indicates that this ifaceType object should not be included.
There are many ways to use this TypeFilter delegate to search for interfaces implemented on a typehere are just a few other searches that can be performed:
See AlsoSee Recipe 9.8; see the "Delegate Class" and "Type.FindInterfaces Method" topics in the MSDN documentation. |