1

I'm trying to make a lightweight version of some strongly-typed DataRows in order to write a test for a method that takes IEnumerable<T> where T : DataRow.

I would like to make a class that inherits from DataRow but has additional properties, as in the autogenerated strongly-typed DataSet.Designer.cs. I cannot get their code to work, and indeed I don't understand how it could work:

// from AnimalDataSet.Designer.cs:
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
public AnimalRow AddAnimalRow(
        string Name, 
        int Species_ID) {
    AnimalRow rowAnimalRow = ((AnimalRow)(this.NewRow()));
    object[] columnValuesArray = new object[] {
        null,
        Name,
        Species_ID};
    rowAnimalRow.ItemArray = columnValuesArray;
    this.Rows.Add(rowAnimalRow);
    return rowAnimalRow;
}

Every time I try to run an imitation of this - I get InvalidCastException (Unable to cast object of type System.DataRow to type AnimalRow). As I would have expected.

So what makes their code more special?

sq33G
  • 3,320
  • 1
  • 23
  • 38
  • what is your datatable? and AnimalDataTable? or a regular vanilla DataTable? the above relies on the overridden `NewRow`, etc. – Marc Gravell Nov 30 '11 at 07:58
  • also... typed datatables.... YMMV, but IMO (very subjective) you should try to just use a regular class here. – Marc Gravell Nov 30 '11 at 08:00
  • ... Designer.cs does not contain any overrides for `NewRow`. Do you think I should upload the whole auto-generated file? I don't think anyone would have the patience for that. – sq33G Nov 30 '11 at 08:00
  • I'm testing a method that takes `IEnumerable where T : DataRow`. IE, no touchy on the method. I'd love to kick all DataTables and their brethren out of the code base. No can do. – sq33G Nov 30 '11 at 08:02
  • Could show us the code which causes the InvalidCastException pls? – Pilgerstorfer Franz Nov 30 '11 at 08:02
  • 1
    @sq33G please don't (upload all). I haven't used typed datatables in a **really** long time, but I'm sure *somewhere* there is a line in there relating to AnimalRow - maybe via a typeof(...) – Marc Gravell Nov 30 '11 at 08:02
  • @PilgerstorferFranz I think we can deduce: the NewRow line – Marc Gravell Nov 30 '11 at 08:04
  • actually, if you wanted to upload it to, say, pastie.org, we could see, without polluting the question? – Marc Gravell Nov 30 '11 at 08:04
  • Actually, you already answered. I'm testing and then I'll show you what the issue was. – sq33G Nov 30 '11 at 08:11

1 Answers1

3

Credit to @Marc Gravell for steering this in the right direction:

The AnimalDataTable class contains two overrides of undocumented* virtual methods of DataTable:

[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
protected override global::System.Data.DataRow NewRowFromBuilder(global::System.Data.DataRowBuilder builder) {
    return new AnimalRow(builder);
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
protected override global::System.Type GetRowType() {
    return typeof(AnimalRow);
}

*mostly undocumented

sq33G
  • 3,320
  • 1
  • 23
  • 38