O'Reilly logo

C# Cookbook by Jay Hilyard, Stephen Teilhet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

12.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 belongs to an interface.

Solution

Use the same technique outlined in Recipe 12.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)
{
    // delegate to the interface based one passing blank
    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 asmType in asm.GetTypes( )) { if (asmType.IsInterface && (asmType.FullName.Equals(interfaceName) || interfaceName.Equals("*"))) { if (asmType.GetMember(memberName, MemberTypes.All, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase).Length > 0) { foreach(MemberInfo iface in asmType.GetMember(memberName, MemberTypes.All, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase)) { Console.WriteLine("Found member {0}.{1}", ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required