0

Possible Duplicate:
Reflecting over all properties of an interface, including inherited ones?

While trying to receive information about all methods from an interface, I first tried the following:

interface IBaseInterface
{
    void SomeMethod();
}
interface ISomeInterface : IBaseInterface {}

Type interfaceType = typeof( ISomeInterface );
BindingFlags allInstanceMembers = BindingFlags.FlattenHierarchy |
                                  BindingFlags.Instance |
                                  BindingFlags.NonPublic |
                                  BindingFlags.Public;
MethodInfo[] methods = interfaceType.GetMethods( allInstanceMembers );

This results in an empty methods array.

I believe the problem is flatten hierarchy doesn't work for interfaces, but I'm not sure. Before attempting an implementation using Type.GetInterfaceMap it would be nice if someone could confirm, or explain what I'm doing wrong.

Community
  • 1
  • 1
Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161

2 Answers2

2

FlattenHierarchy only affects static methods in concrete types:

From http://msdn.microsoft.com/en-us/library/4d848zkb.aspx:

Specify BindingFlags.FlattenHierarchy to include public and protected static members up the hierarchy; private static members in inherited classes are not included.

You will indeed have to use GetInterfaceMap() and/or simply iterate through all interfaces within the hierarchy yourself.

Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
1

I'd recommend checking out Fasterflect, a library intended to make reflection easier and faster (the latter doesn't apply to lookups though).

To find all instance members you can simply do:

var methods = typeof(ISomeInterface).Methods( Flags.InstanceAnyVisibility );

Fasterflect will flatten the hierarchy by default, so no special code is needed to include the base interface members (lookups are recursive, object members not included).

Morten Mertner
  • 9,414
  • 4
  • 39
  • 56
  • Thanks, that library seems to do a great deal of similar things I have already written. :/ – Steven Jeuris Jun 05 '11 at 00:14
  • @Steven If you have something we don't, feel free to join in and contribute.. or just settle for free maintenance ;) PS: I am a contributor to said library. – Morten Mertner Jun 05 '11 at 00:17
  • Every time I read that name I think it's a misspelling of "Fastreflect". :) – Joel B Fant Jun 05 '11 at 00:19
  • @Joel lol, maybe that was the name of 1.0, before it got even faster ;) – Morten Mertner Jun 05 '11 at 00:24
  • @Morten: I'll check it out and see whether it would be interesting to merge features I find missing (if any) over to this library. I've been pondering about splitting all reflection code to a separate project for a while, using an existing one would make more sense. ;p When interested, I already described [some of my more advanced implementations](http://whathecode.wordpress.com/2011/05/25/creating-delegates-during-reflection-for-unknown-types/) on my blog. – Steven Jeuris Jun 05 '11 at 00:29
  • @Morten: At the moment I am working on Emit code [which generates a wrapper class at runtime](https://github.com/Whathecode/Framework-Class-Library-Extension/blob/master/Whathecode.System/Reflection/Emit/EmitHelper.cs) using [RunSharp](http://www.codeproject.com/KB/dotnet/runsharp.aspx). It still needs to be tested and expanded a bit, but I already am using it to generate wrapper classes which can be used during reflection where complete generic types aren't known. I will blog about this pretty soon. – Steven Jeuris Jun 05 '11 at 00:31
  • @Steven Looks like you're quite the reflection wizard already :) I'm actually not sure what the state of our generic support is, but afaicr we simply generate delegates with object parameters and then emit IL to deal with casting once invoked, which sort of puts the responsibility on the caller. I'm off to watch a movie with my better half, but will look at your blog tomorrow :) And just ping me if you'd like to talk or contribute :) – Morten Mertner Jun 05 '11 at 00:44