Type.GetMembers() on an interface does not return members from inherited interfaces
Type.GetMembers() on an interface does not return members from inherited interfaces
Consider the following:
| |
using System; using System.Reflection; public interface IBaseBlah { string GetSuperString(); } public interface IBlah : IBaseBlah { object DoStuff(string name); }
public class Foo { public static void Main() { Type t = typeof(IBlah); foreach(MemberInfo info in t.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) Console.WriteLine(info.Name); } }
|
The output here only gives me "DoStuff". The method on the base interface, GetSuperString(), is not returned. Why not How can I retrieve all methods on an interface, including those provided by base interfaces
Type.GetMembers() on an interface does not return members from inherited interfaces
Shane Gidley
It could list the same members more than once if interface is declared as a "base" for more than one interface
ebc
Not sure if it will work, but have you tried throwing in a BindingFlags.FlattenHierarchy into your binding flags
-Shawn
Najm
class A { }
class B : A {}
interface I {}
class D : I {}
interface I2 : I {}
B inherits from A
D promises to implement I (and probably does)
I2 forces whoever implements I2 to also implement I
The last point is important because there is no interface inheritance in CLI
coronaride
aalford
I did that when trying to figure out a solution myself. It doesn't help, and the help for FlattenHierarchy says it only works for static members.
I wrote a little helper method for the workaround taumuon suggested:
public List<MemberInfo> GetMemberList(Type type)
{
List<MemberInfo> result = new List<MemberInfo>();
result.AddRange(type.GetMembers(BindingFlags.Public | BindingFlags.Instance));
foreach (Type baseInterface in type.GetInterfaces())
{
result.AddRange(GetMemberList(baseInterface));
}
return result;
}
Jeff Walsh
Johan Delimon
I'm not sure why GetMethods() understands when a class derives from (extends in the MSIL) another class, but doesn't understand when an interface implements another one.
As a workaround, you could recursively use the Type.FindInterfaces method on IBlah to find all implemented interfaces and their methods.