2

我正在尝试实现我自己的DataRow课程。和DataRowDataTable如下所示:

public class GenericFormFieldDataTable : TypedTableBase<GenericFormFieldDataRow>
{
    public IEnumerable<ExtraInfo> Information { get; set; }

    public GenericFormFieldDataTable(IEnumerable<ExtraInfo> info)
    {
        Information = info;
    }

    public void AddGenericFormFieldRow(GenericFormFieldDataRow row)
    {
        Rows.Add(row);
    }
    public GenericFormFieldDataRow AddGenericFormFieldRow(LineData data)
    {
        //THE LINE BELOW CAUSES THE CRASH
        GenericFormFieldDataRow newRow = ((GenericFormFieldDataRow)(NewRow()));
        List<object> values = new List<object>();
        ...

        Here we do things with values, Information and eventually fill datarow.LineData

        ...
        newRow.ItemArray = values.ToArray();
        Rows.Add(newRow);
        return newRow;
    }


    protected new GenericFormFieldDataRow NewRowFromBuilder(DataRowBuilder builder)
    {
        return new GenericFormFieldDataRow(builder);
    }

    protected override Type GetRowType()
    {
        return typeof (GenericFormFieldDataRow);
    }

    public GenericFormFieldDataRow this[int index]
    {
        get
        {
            return ((GenericFormFieldDataRow)(this.Rows[index]));
        }
    }
}

public class GenericFormFieldDataRow : DataRow
{
    public LineData LineData { get; set; }

    protected internal GenericFormFieldDataRow(DataRowBuilder builder) : base(builder) {}
}

问题是当我们调用AddGenericFormFieldRow我们得到错误:

Attempted to access an element as a type incompatible with the array.

Call stack:
System.Data.DataTable.NewRow(Int32 record) +60
   System.Data.DataTable.NewRow() +16
   GenericFormFieldDataTable.AddGenericFormFieldRow(LineData data) in GenericFormFieldDataTable.cs:37

该代码基于数据集设计器生成的内容。我在这里错过了什么/做错了什么?

4

0 回答 0