1

を受け取るメソッドのテストを作成するために、いくつかの厳密に型指定された DataRows の軽量バージョンを作成しようとしていますIEnumerable<T> where T : DataRow

DataRow自動生成された厳密に型指定された DataSet.Designer.cs のように、から継承するが追加のプロパティを持つクラスを作成したいと考えています。私は彼らのコードを機能させることができず、実際にどのように機能するのか理解できません:

// 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;
}

これを模倣して実行しようとするたびに、InvalidCastException (System.DataRow 型のオブジェクトを AnimalRow 型にキャストできません) が発生します。私が期待していたように。

では、彼らのコードをより特別なものにしている理由は何でしょうか?

4

1 に答える 1

3

これを正しい方向に導いてくれた@Marc Gravellの功績:

このAnimalDataTableクラスには、文書化されていない* の仮想メソッドの 2 つのオーバーライドが含まれています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);
}

*ほとんど文書化されていない

于 2011-11-30T08:30:30.347 に答える