14

I have an instance of System.Type that represents an interface, and I want to get a list of all the properties on that interface -- including those inherited from base interfaces. I basically want the same behavior from interfaces that I get for classes.

For example, given this hierarchy:

public interface IBase {
    public string BaseProperty { get; }
}
public interface ISub : IBase {
    public string SubProperty { get; }
}
public class Base : IBase {
    public string BaseProperty { get { return "Base"; } }
}
public class Sub : Base, ISub {
    public string SubProperty { get { return "Sub"; } }
}

If I call GetProperties on the class -- typeof(Sub).GetProperties() -- then I get both BaseProperty and SubProperty. I want to do the same thing with the interface, but when I try it -- typeof(ISub).GetProperties() -- all that comes back is SubProperty.

I tried passing BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy to GetProperties, since my understanding of FlattenHierarchy is that it's supposed to include members from base classes, but the behavior was exactly the same.

I suppose I could iterate Type.GetInterfaces() and call GetProperties on each one, but then I would be relying on GetProperties on an interface to never return base properties (since if it ever did, I'd get duplicates). I'd rather not rely on this behavior without at least seeing it documented.

How can I either:

  • Get a list of all the properties on an interface, including those from its base interfaces? Or
  • At least be confident that what I'm seeing is documented behavior that I can rely on, so I can work around it?
Joe White
  • 94,807
  • 60
  • 220
  • 330
  • 1
    possible duplicate of [GetProperties() to return all properties for an interface inheritance hierarchy](http://stackoverflow.com/questions/358835/getproperties-to-return-all-properties-for-an-interface-inheritance-hierarchy) – nawfal Apr 29 '13 at 14:17

2 Answers2

7

An answer of sorts is to be found in an annotation to the .NET framework version 3.5-specific MSDN page on GetProperties(BindingFlags bindingFlags) :

Passing BindingFlags.FlattenHierarchy to one of the Type.GetXXX methods, such as Type.GetMembers, will not return inherited interface members when you are querying on an interface type itself.

[...]

To get the inherited members, you need to query each implemented interface for its members.

Example code is also included. This comment was posted by a Microsoftie, so I would say you can trust it.

AakashM
  • 62,551
  • 17
  • 151
  • 186
  • The "community addition" you mention is not shown for the current version of that page - you have to go back to the .Net Framework 3.5 version. Here's a direct link: http://msdn.microsoft.com/en-us/library/kyaxdd3x%28v=vs.90%29.aspx – RenniePet Jul 25 '14 at 09:42
1

See here: GetProperties() to return all properties for an interface inheritance hierarchy

I don't think it's possible to get all members without doing what you suggested (i.e. getting all the implementing interfaces.)

Community
  • 1
  • 1
BFree
  • 102,548
  • 21
  • 159
  • 201