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
}

      

+3


source to share


5 answers


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)



+2


source


From MSDN:

typeof(IList).GetMembers()

      



Gets the elements (properties, methods, fields, events, etc.) of the current type.

+7


source


You just need to query 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
}

      

+2


source


var t = typeof(IMyInterface);
foreach( var m in t.GetMethods() ){
    Debug.WriteLine(m.Name);
}

      

+2


source


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;

      

+1


source







All Articles