9.5. An Advanced Interface Search Mechanism
Problem
You 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. Youneed 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.
Solution
Use LINQ to query the type interface information and perform rich searches. The method shown in Example 9-3 will demonstrate one complex search that can be performed with LINQ.
Example 9-3. Performing complex searches of interfaces on a type
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; public class SearchType { public static void FindSpecificInterfaces() { // set up the interfaces to search for Type[] interfaces = { typeof(System.ICloneable), typeof(System.Collections.ICollection), typeof(System.IAppDomainSetup) }; // set up the type to examine Type searchType ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access