This article is similar to the previous one Finding Members in an Assembly. This time, use the same technique as previouly, but filter out all types except interfaces. This article also makes use of the GetMember method of the System.Type class. The name may contain a * 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.

The following shows two overloaded method of FindIFaceMemberInAssembly. The first overloaded version 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}",
                        asmType.ToString(),iface.ToString());
                }
            }
        }
    }
}

The FindIFaceMemberInAssembly method operates very similarly to the FindMemberInAssembly method in the previous article. The main difference 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.

Popularity: 3% [?]