Sorry about my first answer, at first I tried getting to the method through the InterfaceMap
structure, and the methods in that structure actually do report the interface as the DeclaringType
. Then I tinkered with it and didn't realize it was subtly broken using the method you'd posted to get the MethodInfo
.
The upside, though, is that this subtle difference led to the realization that the MethodInfo
for the interface method, and the MethodInfo
for the implementing method, are not actually the same method. And upon further thought, I am actually pretty sure that what you want to do is impossible to do reliably.
Explicit interface implementations in C# are a bit of syntactic sugar over the way it really works in the CLR. In other languages, a single concrete method can implement multiple interface methods the same way a concrete class can implement multiple interface types. For example, in VB.NET, it's perfectly legal to do this:
Public Overrides Function Baz() As String Implements IFoo.Foo, IBar.Bar
Here you have one method that's explicitly implementing two interface methods. If it were possible to get the original interface class - or even the original interface method - which one would you get? How do you resolve the ambiguity?
I'm not an expert on the CLR internals, but I believe that interface maps are one-way. When you implement an interface or method, implicitly or explicitly, the compiler creates a map from the interface to the implementation, and that's all it ever needs to handle method calls against the interface.
Think of the C#-generated method names as being almost coincidental. Even though the method name is System.Collections.IEnumerable.GetEnumerator
, that really is just a name, and there is not actually any information about System.Collections.IEnumerable
encoded in the method itself.