5

Is there a way to get a list of interface members? I know about System.Reflection.MemberInfo, but it includes everything in an object, not just a certain interface.

Here is the program, I'm not sure how to get you the interface as I didn't write it, but it is part of the Ascom Standard (http://ascom-standards.org).

public static void Test1()
{
    Console.WriteLine("mark1"); // this shows up...
    var type = typeof(Ascom.Interface.ITelescope);
    var members = type.GetMembers();
    Console.WriteLine(members.Count); // gives 0
    foreach (var member in members)
    {
        Console.WriteLine(member.Name); //nothing from here
    }
    Console.WriteLine("mark4"); // ...as well as this
}
Arlen Beiler
  • 15,336
  • 34
  • 92
  • 135
  • To clarify, you have two `Type` objects, one for a `class` (or `struct`) and one for `interface` and you want to get the methods that implement the interface in the class. Is that right? Why do you want them? – svick Mar 09 '12 at 21:32
  • I just want the interface list. I'm trying to dynamically implement the interface at runtime, fyi. – Arlen Beiler Mar 09 '12 at 21:43
  • Oops, interface *member* list. – Arlen Beiler Mar 09 '12 at 22:12

5 Answers5

8

From MSDN:

typeof(IList).GetMembers()

Gets the members (properties, methods, fields, events, and so on) of the current Type.

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
  • If you want to filter to just the properties from this, you can use Linq: `typeof(IList).GetMembers().OfType()`. – gabe May 17 '19 at 17:54
3

If it is a COM interface, then you should disable "Embed Interop Types", otherwise it will only insert used members in the assembly. I guess you don't use any methods/properties from that interface in the assembly, that's why they never get inserted, so you can list them with reflection. (Thx OWO)

2

You just need to ask for the members for the interface type:

var type = typeof(IConvertible);
var members = type.GetMembers();

foreach (var member in members)
{
    Console.WriteLine(member.Name); // ToInt32 etc
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @Arlene Beiler: For which interface? It works fine for me... I tried it with the code I posted. – Jon Skeet Mar 09 '12 at 21:50
  • Would it have anything to do with the interface being marked public rather than its members? When I browse it in the object browser, it says the interface is public, but none of the members are marked as public. – Arlen Beiler Mar 09 '12 at 21:51
  • 2
    @ArlenBeiler: Interface members are implicitly public. If you can give us a short but complete program which demonstrates the problem, that would make life easier... – Jon Skeet Mar 09 '12 at 21:56
  • if I ask for `members.length` it gives me 0 – Arlen Beiler Mar 09 '12 at 22:04
  • By the way, I tried it with `IConvertible` and it worked fine. – Arlen Beiler Mar 09 '12 at 22:06
  • I see what you mean about interfaces being public. `IConvertible` shows up exactly the same in the object browser. – Arlen Beiler Mar 09 '12 at 22:10
2
var t = typeof(IMyInterface);
foreach( var m in t.GetMethods() ){
    Debug.WriteLine(m.Name);
}
Phil
  • 42,255
  • 9
  • 100
  • 100
1

To get a list of members of some interface, you can just do that: list members of that interface, as others pointed out:

var members = interfaceType.GetMembers();

But if you want to know what members of certain type implement that interface, you can use GetInterfaceMap() and examine the TargetMethods field:

var members = type.GetInterfaceMap(interfaceType).TargetMethods;
svick
  • 236,525
  • 50
  • 385
  • 514