
Signature
<http://www.midnightbeach.com> Contracting, consulting, training
.NET 2.0 for Delphi Programmers <http://www.midnightbeach.com/.net>
In production - in stores by June
I'm referring specifically to how the reflection API methods (ie GetMethod,
GetProperty, etc) treat inherited members in interfaces as opposed to
inherited members in classes.
Run the following code. GetMethod finds the inherited method in the derived
class, but not in the derived interface. I realise that you can dig up the
hierachy with reflection to find the inherited members, but the code
performing the reflection is in a third party library we are using.
I suspect this is somehow the by design behaviour rather than a bug. But I'm
trying to understand the reason why the behaviour should differ between
interfaces and classes in this way.
Niall
using System;
using System.Reflection;
namespace ReflectionTest
{
class Program
{
static void Main(string[] args)
{
MethodInfo iMi = typeof(IChild).GetMethod("DoParent", BindingFlags.Public
| BindingFlags.Instance);
MethodInfo mi = typeof(Child).GetMethod("DoParent", BindingFlags.Public |
BindingFlags.Instance);
Console.WriteLine("Interface method: " + ((iMi == null) ? "null" : "not
null"));
Console.WriteLine("Class method: " + ((mi == null) ? "null" : "not
null"));
Console.ReadLine();
}
}
public interface IParent
{
void DoParent();
}
public interface IChild : IParent
{
void DoChild();
}
public class Parent : IParent
{
public void DoParent()
{
}
}
public class Child : Parent, IChild
{
public void DoChild()
{
}
}
}
> > The reason it fails is that members on interfaces inherited from another
> > interface don't seem to be returned from the reflection methods. This is not
[quoted text clipped - 13 lines]
> }
>
Jon Shemitz - 04 May 2006 18:24 GMT
> I suspect this is somehow the by design behaviour rather than a bug. But I'm
> trying to understand the reason why the behaviour should differ between
> interfaces and classes in this way.
As per my previous message, I suspect it has to do with the nature of
interface inheritance. A derived interface doesn't act like a flat set
of methods and properties; it acts like a collection of methods and
properties and interfaces. You can see this in the implementation
behavior (where you have to implement IBase.Foo, not IDerived.Foo) and
you're seeing this in Reflection.
Why? I don't really know - but I guess it's at least partly
implementation driven. Each interface is its own vtable and possibly
(as in, I haven't looked, and [sorry!] don't have the time to look
this morning) ancestral interfaces are implemented as pointers to the
ancestral vtable, not as spaces within the derived vtable. This would
save space, ay some slight runtime cost.

Signature
<http://www.midnightbeach.com> Contracting, consulting, training
.NET 2.0 for Delphi Programmers <http://www.midnightbeach.com/.net>
In production - in stores by June
Ben Voigt - 04 May 2006 19:11 GMT
> I'm referring specifically to how the reflection API methods (ie
> GetMethod,
> GetProperty, etc) treat inherited members in interfaces as opposed to
> inherited members in classes.
I think because if you want the implementing method, you use
GetInterfaceMap.
> Run the following code. GetMethod finds the inherited method in the
> derived
[quoted text clipped - 75 lines]
>> void IBase.Foo() {} // can't be IDerived.Foo
>> }