14

I need to be able to determine if a given method or property comes from a particular interface and is explicitly implemented.
Has anyone done this and is it actually possible to get this information by the means of .NET reflection?


Update

As can be seen in comments below the accepted answer, the actual thing I am trying to accomplish is to call the method that implements a particular interface via reflection. Since the possibility to have multiple interfaces with the same method signature, I wanted to determine the right implementation to invoke based on the interface. In my scenario, the implementation type, interface and method name are determined at runtime, so I cannot use simple casting in my case.

Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
  • Just out of curiosity, why do you need to know if it's explicitly implemented, of if it's implicit? – Tipx Jul 04 '11 at 14:18
  • Do you need to distinguish implicit/explicit interface implementation or type's own method/ interface's method? – abatishchev Jul 04 '11 at 14:32
  • @Tipx, Here is a case: If you have interface with attributes applied to its members, an inherited class will not get those attributes on the implemented properties/methods. Therefore, I must loop through the interfaces of a particular type to get the attributes (reflection on interfaces gives that attribute information). Here comes the problem - if I have 2 or more interfaces defining the same method, each interface explicitly implemented to provide different logic? I'd like to determine if the particular implementation (the one decorated with the attribute) is explicitly implemented or not. – Ivaylo Slavov Jul 04 '11 at 14:33
  • @abatishchev, I want to know if a class explicitly implements a member of an interface. – Ivaylo Slavov Jul 04 '11 at 14:36
  • @Ivaylo Oh, interesting. Thanks :-) – Tipx Jul 04 '11 at 15:10
  • possible duplicate of [How do I use reflection to get properties explicitly implementing an interface?](http://stackoverflow.com/questions/278997/how-do-i-use-reflection-to-get-properties-explicitly-implementing-an-interface) – nawfal Jan 12 '15 at 20:39
  • @nawfal, my goal was to _invoke_ the explicitly implemented member, rather than just query it. As it appears in the accepted answer, there is no relevance to whether its declaration is being explicit or not, thus I think it is not a duplicate – Ivaylo Slavov Jan 13 '15 at 09:15

2 Answers2

17

Explicitly implemented interface methods in C# are private in the target class. You can use this fact and create this extension method to return only these methods:

static IEnumerable<MethodInfo> GetExplicitlyImplementedMethods(this Type targetType, 
    Type interfaceType) 
{ 
  return targetType.GetInterfaceMap(interfaceType).TargetMethods.Where(m => m.IsPrivate);
}

Note: this is for C# only.

UPDATE: But, from your requirements, it seems that you only want to know which methods implement which interface methods, without really caring about whether the implementation is implicit or explicit. For a solution that works across languages then, this would suffice:

static IEnumerable<MethodInfo> GetImplementedMethods(this Type targetType,
    Type interfaceType) 
{ 
  return targetType.GetInterfaceMap(interfaceType).TargetMethods;
}
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
Jordão
  • 55,340
  • 13
  • 112
  • 144
  • 2
    This is only true for for programs complied with C#, as VB.Net implements all interface members explicitly, and allows them to be public. See https://gist.github.com/1063434 Note how every method contains '.override IBob::Method'. – Chris Chilvers Jul 04 '11 at 15:01
  • @Chris: in VB.NET, you _can_ state explicitly which method a class method implements, and it can have any accessibility or name. The code is designed for C# _only_. – Jordão Jul 04 '11 at 15:06
  • 1
    Indeed, I just thought it worth pointing out for anyone who finds this answer but hasn't considered other languages when applying this heuristic. – Chris Chilvers Jul 04 '11 at 15:09
  • @Chris, from what you wrote it seems that VB.NET should behave quite differently. I assumed that whatever .NET language is used, the code should be compiled to the same IL, therefore these reflection concerns should not be language dependent. Can you please provide me with a link to some related information? Indeed if here really is such a drastic difference I would like to inform myself more on this topic. Thanks in advance. – Ivaylo Slavov Jul 05 '11 at 09:04
  • 1
    @Ivaylo: in VB.NET, any member can explicitly implement an interface member with any accessibility or even with a different name. From your "requirements", it seems that you just need to know which methods implement which interface methods, which `GetInterfaceMap` return to you nicely. No need to worry about explicit or implicit interface implementations. – Jordão Jul 05 '11 at 12:51
  • 1
    @Jordão, I see that VB.NET provides quite a lot more freedom on explicit interface implementation. I became worried for a moment if your solution will work fine on VB.NET assemblies, but now I realize I actually do not need to know if the implementation is explicit. I just wanted to call an implementation of a concrete interface (to avoid ambiguity when multiple interfaces exist) and your corrected version is again right on the mark. Thank you for the extra efforts. – Ivaylo Slavov Jul 05 '11 at 14:00
1

If when using reflection a method is private and its name contains a ., like "System.IDisposable.Dispose", then it is an explicit implementation.

Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74
  • Although it may not be true for dynamically generated methods, it is usually a good indicator. – IS4 Nov 02 '14 at 17:21