tags:

views:

19

answers:

1

I'm creating a table from a dynamically created IBindingList using

class TableBuilder
{
    private Type m_TableType;
    // ...  create and define m_TableType here
    public IBindingList CreateTable()
    {
       return Activator.CreateInstance(m_TableType) as IBindingList;
    }
}

class DynamicTable : IBindingList
{
    private IBindingList m_theList;
    private TableBuilder m_tableBuilder;

    public DynamicTable(TableBuilder tableBuilder)
    {
        m_tableBuilder = tableBuilder; 
        m_theList = tableBuilder.CreateTable();
    }

    public void LoadData()
    {
        // ...
    }
}

I would like to promote the IBindingList functionality of m_theList to the level of the class so I can make calls like

    var myTable = new DynamicTable(someTableBuilder);
    int count = myTable.Count;
    myTable.LoadData();
    count = myTable.Count;

How can I get all the m_theList public members to be members of DynamicTable. I can not derive DynamicTable from m_TableType since it is only known at run time.

-Max

A: 

You will have to do it as old subclassing, implement the interface and in each method call the corresponding method in m_theList:

    //methods
    public void AddIndex(PropertyDescriptor property)
    {
        m_theList.AddIndex(property);
    }

    public object AddNew()
    {
        return m_theList.AddNew();
    }

    //properties

    public bool AllowEdit
    {
        get { return m_theList.AllowEdit; }
    }

    ....
    //for events you can use add/remove syntax 

    public event ListChangedEventHandler ListChanged
    {
        add { m_theList.ListChanged += value; }
        remove { m_theList.ListChanged -= value; }
    }

    ....
    //indexer...
    public object this[int index]
    {
        get
        {
            return m_theList[index];
        }
        set
        {
            m_theList[index] = value;
        }
    }
jmservera
Since this causes extra overhead for low level calls, I'm also investigating deriving the DynamicTable from BindingList<T>
Max Yaffe