How to get a list of interface members
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 specific interface.
Here is a program, I'm not sure how to get 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
}
source to share
If it is a COM interface, you should disable Insert Interop Types, otherwise it will only insert usable items into the assembly. I think you are not using any methods / properties from this interface in the assembly, so they are never inserted, so you can enumerate them with reflection. (thanks OWO)
source to share
To list the members of some interface, you can simply do this: list the members of that interface, as others have pointed out:
var members = interfaceType.GetMembers();
But if you want to know which elements of a particular type implement this interface, you can use GetInterfaceMap()
and examine the fieldTargetMethods
:
var members = type.GetInterfaceMap(interfaceType).TargetMethods;
source to share