Recipe13.5.Finding Members Within an Interface


Recipe 13.5. Finding Members Within an Interface

Problem

You need to find one or more members with a specific name, or a part of a name, that belong to an interface.

Solution

Use the same technique outlined in Recipe 13.4, but filter out all types except interfaces. The first overloaded version of the FindIFaceMemberInAssembly method finds a member specified by the memberName parameter in all interfaces contained in an assembly. Its source code is:

 public static void FindIFaceMemberInAssembly(string asmPath, string memberName) {     // Delegates to the overloaded FindIFaceMemberInAssembly method     // passing in a wildcard character as the interfaceName param.     FindIFaceMemberInAssembly(asmPath, memberName, "*"); } 

The second overloaded version of the FindIFaceMemberInAssembly method finds a member in the interface specified by the interfaceName parameter. Its source code is:

 public static void FindIFaceMemberInAssembly(string asmPath, string memberName,     string interfaceName) {     Assembly asm = Assembly.LoadFrom(asmPath);     foreach(Type type in asm.GetTypes( ))     {         if (type.IsInterface &&            (type.Name.Equals(interfaceName) ||                                    interfaceName.Equals("*")))         {             MemberInfo[] members = type.GetMember(memberName, MemberTypes.All,                          BindingFlags.Instance | BindingFlags.NonPublic |                          BindingFlags.Public | BindingFlags.Static |                          BindingFlags.IgnoreCase);             if (members.Length > 0)             {                 foreach(MemberInfo iface in members)                 {                     Console.WriteLine("Found member {0}.{1}",                         type.ToString( ),iface.ToString( ));                 }             }         }     } } 

Discussion

The FindIFaceMemberInAssembly method operates very similarly to the FindMemberInAssembly method of Recipe 13.4. The main difference between this recipe and the one in Recipe 13.4 is that this method uses the IsInterface property of the System.Type class to determine whether this type is an interface. If this property returns TRue, the type is an interface; otherwise, it is a noninterface type.

This recipe also makes use of the GetMember method of the System.Type class. This name may contain an * wildcard character at the end of the string only. If the * wildcard character is the only character in the name parameter, all members are returned.

If you'd like to do a case-sensitive search, you can omit the BindingFlags.IgnoreCase flag from the call to Type.GetMember.

See Also

See Recipes 13.4 and 13.10; see the "Assembly Class," "BindingFlags Enumeration," and "MemberInfo Class" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net