Inherited Interface and GetProperties()

  • Thread starter Thread starter Jason Cartwright
  • Start date Start date
J

Jason Cartwright

Apparently there is a bug in the Reflection methods used to retrieve
inherited members of an interface. A post almost a year ago detailed
the exact symptoms I am experiencing.

What I need to do is create a list of objects dynamically, using
Activator.CreateInstance(). I have a SqlDataReader that I am
iterating. I want to use the Properties defined on an interface to
retrieve the values from the reader. My classes have several inherited
properties that I want to ignore because the reader.GetOrdinal()
method throws an exception if the column doesn't exist.

My first interface contains about 30 properties and a derived
interface is used to define an additional property. Using the second,
derived interface, I only get the additional property when using the
GetProperties() method. This method works fine on my derived classes,
but it fails on my derived interfaces.

Anyway, I'm less than optimistic, but is there a way accomplish this?

Thanks,
Jason
 
Jason,
Apparently there is a bug in the Reflection methods used to retrieve
inherited members of an interface.

I don't think it's a bug. While high level languages such as C# and
VB.NET makes it look like an interface can inherit from other
interfaces, it's represented as interface implementation in metadata.
For example, if you write this in C#

interface IFoo {}
interface IBar : IFoo {}

In IL assembler syntax IBar becomes

..class interface private abstract IBar implements IFoo {}

So from the runtime's point of view, we're dealing with
implementation, not inheritance.

Anyway, I'm less than optimistic, but is there a way accomplish this?

Call GetInterfaces() on the "most derived" interface type to get the
interfaces it implements, and call GetProperties on them, and so on.



Mattias
 
That is very helpful information. Thanks for the response.

I will create a recursive function to query for the properties through
the inheritance chain. However, is there an easy way to terminate the
recursion at a namespace boundary?

Thanks,
Jason
 
Back
Top