将字段的IBindingList提升为类

时间:2010-09-14 21:12:14

标签: c# dynamic

我正在使用

从动态创建的IBindingList创建一个表
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()
    {
        // ...
    }
}

我想将m_theList的IBindingList功能提升到类的级别,这样我就可以调用

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

如何让所有m_theList公共成员成为DynamicTable的成员。我无法从m_TableType派生DynamicTable,因为它只在运行时才知道。

-Max

1 个答案:

答案 0 :(得分:0)

您必须将其作为旧的子类,实现接口并在每个方法中调用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;
        }
    }