我正在尝试实现自己的DataRow
课程。 DataRow
和DataTable
类看起来像这样:
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
代码基于数据集设计器生成的内容。我在这里错过了什么/做错了什么?