Search code examples
data-bindingreflectioninterfacemultiple-inheritancetypedescriptor

Binding IList<IMyInterfaceType> doesn't display members of Interfaces that IMyInterface inherits


I'm binding IList to a GridView. IMyInterface looks like

public interface IMyInterface: IHasTotalHours, IHasLines
{
    DateTime GoalStartDate { get; set; }
    DateTime GoalEndDate { get; set; }
}

I bind an instance to a Grid like this:

IList<IMyInterface> instance= GetMyData();

myGrid.DataSource = instance;
myGrid.DataBind();

When bind this to the grid, the only members that show up in the grid are the direct members of IMyInterface: GoalStartDate and GoalEndDate.

Why is that? How do I get the grid to display the members of the other interfaces it inherits?

Update The inherited interfaces define simple data properties like

public interface IHasTotalHours
{
    string Description { get; set; }
    int Hours{ get; set; }
}
public interface IHasLines
{
    double TotalLines { get; set; }
    double LinesPerHour { get; set; }
}

There is a class that implements IMyInterface:

public class MyClass : IMyInterface
{
    public string Description { get; set; }
    public int Hours { get; set; }
    public double TotalLines { get; set; }
    public double LinesPerHour { get; set; }
    public DateTime GoalStartDate { get; set; }
    public DateTime GoalEndDate { get; set; }

}

These are cast as IMyInterface, and returned in the list that I'm binding to the GridView.


Solution

  • Data bound controls do not use reflection but a TypeDescriptor to get the properties from a data source. In the TypeDescriptor.GetProperties method, you can read the following:

    The properties for a component can differ from the properties of a class, because the site can add or remove properties if the component is sited.

    Apparently the default implementation will only return direct properties from an Interface and not the inherited ones.

    Luckily this mechanism is extensible, and you can write a TypeConverter class with custom property information implementation. Please refer to the remarks in the TypeConverter documentation for implementing property logic.

    The GetProperties implementation of your custom TypeConverter class can call TypeDescriptor.GetProperties(Type) on your interface and all it's inherited interfaces. But maybe you could even write a generic TypeConverter that would find all inherited properties by using reflection.

    Then you attach this custom TypeConverter to your interface with the TypeConverterAttribute attribute.

    And then, like magic, the data source will find all properties. ;-)